PDA

View Full Version : [AS2] Help With My Game


langel28
02-25-2009, 04:03 AM
Hello out there,

I am making a helicopter game and need some help, the screen size will be 800 by 600 pixels.

Here is the code to move the sprite:

onClipEvent (load) {

moveSpeed = 8;

}

onClipEvent (enterFrame) {

if (Key.isDown(Key.RIGHT)) {

this._x += moveSpeed;

} if (Key.isDown(Key.UP)) {

this._y -= moveSpeed;

} if (Key.isDown(Key.DOWN)) {

this._y += moveSpeed;

} if (Key.isDown(Key.LEFT)) {

this._x -= moveSpeed;

}

}

Can anyone help me add the code that will stop the sprite from going off the screen?

dijup
02-25-2009, 04:17 AM
check the hit area so that you can track wether it is in the screen or not.

ntou45
02-25-2009, 04:53 AM
I'm not sure about tracing if it's off screen or not but I use walls.

Basically, make an MC that's a wall, and give it this actionscript:

if (this.hitTest(_root.[helicoptermc])) {
[helicoptermc]._x+=_root.moveSpeed ;

Of course, change the helicoptermc to whatever's the helicopter's instance name is, and change _x += to _y or -= depending on what side it's on.

basically, the code makes it so that if the helicopter touches a wall, it will move back to where it came from at the same speed, giving it the illusion as if it never moved at all.

then just place the walls slightly off screen and voila, your helicopter will no longer move offscreen

kkbbcute
02-25-2009, 05:54 AM
There is a better way, just use an onEnterFrame function to check the x and y coords of the mc on every frame, and then just return the mc to the last x and y coords on hitTest. Use a variable and alternate it between true and false to capture the coords of the previous frame without overwriting the coords captured with the new coords on the current frame.

Get it?

langel28
02-25-2009, 04:56 PM
I wish my math was better but I'm still confused

langel28
02-25-2009, 04:57 PM
I will try, thanks for the input guys and gals

M1KE
02-25-2009, 05:32 PM
Heres an easy way I worked out:

if(this._x>=800){
this._x=800
}
if(this._x<=0){
this._x=0
}
if(this._y>=600){
this._y=600
}
if(this._y<=0){
this._y=600
}

Also if you want you can make it go off the screen one way and come back on the other side. So man goes off left side of screen comes back right side of screen

if(this._x>=800){
this._x-=800
}
if(this._x<=0){
this._x+=800
}
if(this._y>=600){
this._y-=600
}
if(this._y<=0){
this._y+=600
}

kkbbcute
02-26-2009, 01:42 PM
Instead of repeating 800 and 600 so many times you could just replace it with a variable. That way you just need to change one number everytime.

bluemagica
02-26-2009, 02:13 PM
And i would suggest using stage constants like stage.stageHeight (thats as3, i forgot as2 version of it).....then later on if you nest it within another movieclip or decide to change stage size, you will have lesser recoding to do!

kkbbcute
02-26-2009, 02:35 PM
And i would suggest using stage constants like stage.stageHeight (thats as3, i forgot as2 version of it).....then later on if you nest it within another movieclip or decide to change stage size, you will have lesser recoding to do!

But what if his area isn't the entire stage, but just a proportion, so you could just declare two variables like this: yWidth = stage.stageHeight/2.

bluemagica
02-26-2009, 02:56 PM
thats what i was saying....instead of giving whole constants like 800, 600, most of thime its better to use stage relative values!

kkbbcute
02-26-2009, 03:09 PM
thats what i was saying....instead of giving whole constants like 800, 600, most of thime its better to use stage relative values!

Err.. no, you said to use stage constants, not stage relative values, but I guess thats what you meant anyway:rolleyes: