Groups | Blog | Home
all groups > flash actionscript > august 2005 >

flash actionscript : help with boundries


Thefatman
8/3/2005 11:31:33 PM
i need help with a game i am creating. In this game i hope to have this man
walking down the street shooting things comming at him, i have managed to get
him firing and shooting, BUT, i want him to stop at the edge of the road, i
can't fgure out how to do this. I have already made the road a seperate symbol
and used
onClipEvent(load){
myBounds = _root.road.getBounds(_root);
startDrag(this, true, myBounds.xMin, myBounds.yMin, myBounds.xMax,
myBounds.yMax);
}

this works, but, i dont want it to be dragged around the screen, i want it so
when you press the arrow keys he moves, i have achieved this, and when he gets
to the edge of the road he stops...how do i do this, i am not a master of flash
so it's probably just a silly error on my part.

Alo while im here i want to ask, is there any way in which i can make the
bullets independant of where the gun barrel is pointing, at the moment it
follows the direction the barrel goes, even after fired (i have used a rollOver
action to make it move and put the bullet fire inside he barrel mc)
ok, so i need answers, and i hope i've made it clear what i want cause i
might not have...thankyou.
NSurveyor
8/3/2005 11:34:46 PM
For the first question, look up hitTest() in the ActionScript dictionary
(should be under MovieClip methods). For the second problem, you could use
attachMovie() to create a copy of your bullet on to the stage. And then you
could use an onEnterFrame handler for each bullet that is created. But before I
can help you any further, I suggest you look up these two MovieClip methods.
Thefatman
8/8/2005 11:25:22 PM
NSurveyor
8/9/2005 12:00:00 AM
You can use hitTest. For example, add this script in place of your current
code, for dragging:

onClipEvent (mouseMove) {
if(_root.road.hitTest(_root._xmouse,_root._ymouse,true)){
_x = _parent._xmouse;
_y = _parent._ymouse;
}
}

As long as the mouse is touching the road, the clip can move to where the
mouse is, since the mouse is on the road.
Thefatman
8/10/2005 10:37:13 PM
ocourse...thanks...but umm...what if i didnt want it to foillow my mouse, what
if i just want to use the keyboard to move him left and right and stop at the
edge of the road?...i know it probably seems obvious but...i have no clue
NSurveyor
8/10/2005 10:53:16 PM
If your man movieclip doesnt have an instance name, open up the properties
panel and type in: man_mc for the <Instance Name>. If he does, just replace
man_mc with the correct instance name in the code below. Place this script on
the frame that holds your man.

//Determines how many pixels the man will move while a key is down.
man_mc.speed = 5
//Create a listener object to 'listen' for onKeyDown event.
lo = new Object();
lo.onKeyDown = function(){
//Create variables to determine how much to move in _x and _Y
var movey = 0;
var movex = 0;
//Depending on what arrow keys are pressed, increase/decrease
//movex and movey
if(Key.isDown(Key.LEFT)){
movex-=man_mc.speed;
}
if(Key.isDown(Key.RIGHT)){
movex+=man_mc.speed;
}
if(Key.isDown(Key.UP)){
movey-=man_mc.speed;
}
if(Key.isDown(Key.DOWN)){
movey+=man_mc.speed;
}
//Move the man to the new location
man_mc._x+=movex;
man_mc._y+=movey;
//But if the man isn't on the road anymore...
if(!man_mc.hitTest(_root.road)){
//Move him back!
man_mc._x-=movex;
man_mc._y-=movey;
}
}
//Make the listener object 'listen' to the keys
Key.addListener(lo);

Don't forget to get rid of the old code!
Thefatman
8/11/2005 12:19:56 AM
IT WORKS!!!...thankyou so much thankyou thankyou....


NSurveyor
8/11/2005 12:24:07 AM
You're welcome. I would add more to the move script...

for ex:

if(Key.isDown(Key.SPACE)){
bullet_counter++;
AddThis Social Bookmark Button