Actionscript 2. solid objects

Started by: Tork | Replies: 8 | Views: 1,040

Tork

Posts: 155
Joined: Jul 2008
Rep: 10

View Profile
Feb 27, 2010 6:49 PM #551571
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
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Feb 27, 2010 7:00 PM #551573
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

Posts: 1,127
Joined: Jan 2009
Rep: 10

View Profile
Feb 27, 2010 10:06 PM #551603
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
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Feb 28, 2010 12:23 AM #551651
Quote from doc
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

Posts: 155
Joined: Jul 2008
Rep: 10

View Profile
Feb 28, 2010 10:44 AM #551812
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
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Feb 28, 2010 2:03 PM #551842
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

Posts: 1,127
Joined: Jan 2009
Rep: 10

View Profile
Feb 28, 2010 2:48 PM #551849
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

Posts: 155
Joined: Jul 2008
Rep: 10

View Profile
Feb 28, 2010 3:24 PM #551861
Quote from doc
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

Posts: 1,127
Joined: Jan 2009
Rep: 10

View Profile
Feb 28, 2010 4:30 PM #551885
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