PDA

View Full Version : Actionscript 2. solid objects



Tork
02-27-2010, 11:49 AM
Hi, i'm making a game.. well.. just trying something, I'm really new to actionscript,
im using actionscript 2.
now heres the problem:
i'm having a ball following the cursor, but it can move through solid objects, how do i prevent it from doing that? i have the ball following the cursor, its not moving with the arrow keys.

i use this for the ball follow cursor thingy:

onClipEvent(enterFrame) {
x=_root._xmouse
y=_root._ymouse
_x+=(x-_x)/10
_y+=(y-_y)/10
}

Scorpioxxx
02-27-2010, 12:00 PM
Check for a colision with the object then if it's (the object) y is less than mouse's but greater than the ball's, tell it the ball can't move along the y axis, else/if the y value isn't between the ball and mouse values, it can, same or the x axis and this requires you changing/editing your script.

doc
02-27-2010, 03:06 PM
Scorpio, i dont think that would work for any comlex walls, and that's probably what he's looking for. I would try something like this:



onClipEvent(enterFrame){
while(this._x<_root._xmouse-1&&!_root.wall.hitTest(this._x+radius, this._y, true)){
this._x++;
}
}


That's only for horizontal positive motion (moving from left to right). You can repeat all of this for each end of the MC (right to left, up to down, and down to up).

And the variable "radius" has to be defined as the distance from the middle of the variable to the side. So you could say:

radius=this._width/2;

And the reason I put a -1 in the first half of the while statement is just so that it doesn't flicker back and forth. I'm not honestly sure if this would happen in this case, it would depend on how you coded everything else. Try removing it and you can see.

Does this make any sense?

Scorpioxxx
02-27-2010, 05:23 PM
Scorpio, i dont think that would work for any comlex walls, and that's probably what he's looking for.
Yea plus I think learning your method would be better, ie, no need to recode if he saves the engine and such then wants to re-use it.

On a semi-related note object collisions are far more advanced in AS3 so doing stuff like this can be quite simple if you can pick out the right functions, one of the few advantages I've found.

Tork
02-28-2010, 03:44 AM
Thanks!
sorry for my late respond... i'm going to try it out if it works ^^

EDIT::
thanks for your great help both of you.. but the code, well.. i dind't get any errors or so... but when the mouse touches the wall, the mouse just pushes the wall away.. that's not really what i had in mind, maybe i did something wrong,
i'll just ask it, even it is so nooby as i dont know but anyways:
on what do i have to put that script? on the ball, wall or the frame?
and where do i have to put that additional script that you typed, do i have to replace it with something? or... well, i don't know.
thanks for your time :) and sorry for wasting your time :P

Scorpioxxx
02-28-2010, 07:03 AM
The script from doc that should go in the ball, and the additional bit that defines radius should go in the ball as well, since it will be referencing the ball via "this."

doc
02-28-2010, 07:48 AM
Looking at your questions, you should probably learn some more basic as2 first :D This can be kinda complicated, so you might wanna start with the basics.
and if you're using onClipEvent(enterFrame){ then the radius code will want to be in its separate onClipEvent(load){ so that it will only process once.

Tork
02-28-2010, 08:24 AM
Looking at your questions, you should probably learn some more basic as2 first :D This can be kinda complicated, so you might wanna start with the basics.
and if you're using onClipEvent(enterFrame){ then the radius code will want to be in its separate onClipEvent(load){ so that it will only process once.


Hmh, maybe i should..
i thought that this was pretty basic, would you recommened anything?
looking up tutorials doesn't really help me...

doc
02-28-2010, 09:30 AM
I'll help you out if you want. I think you should understand how to refer to different mc's in a timeline, variables, and the onEnterFrame stuff as opposed to onClipEvent(enterFrame). You can pm me if this doesnt make sense