Saturday, March 27, 2010

Help with code...emergency

Hi!



We are making a game in school, it's a boat crossing the
ocean.

We have a problem that the boat doesn't stop when it gets to
the end of the screen, it just continues moving away.



So, If anyone can help us with the code that makes the boat
stop inside the border... PLEASE



This is the size of the border that we want the boat to stay
inside:



W: 4381.4 X: -2190.7

H: 2352.0 Y: -1176.0





This is the code we have now:



on (keyPress ''%26lt;Left%26gt;'')

{

this._x = this._x + this._parent.boatSpeed;

}



on (keyPress ''%26lt;Right%26gt;'')

{

this._x = this._x - this._parent.boatSpeed;

}



on (keyPress ''%26lt;Up%26gt;'') {

this._y = this._y + this._parent.boatSpeed;

}



on (keyPress ''%26lt;Down%26gt;'') {

this._y = this._y - this._parent.boatSpeed;

}









Thank you in advance!!Help with code...emergency
use an if-statement so you only update the boat's position
when it's within your boundaries.Help with code...emergency
Hi!



Thanks for your answer.... I've tried this code...but I can't
get it to work... can you see what is wrong??





var top:Number=0;

var left:Number=0;

var right:Number=500;

var bottom:Number=500;



on (keyPress ''%26lt;Left%26gt;'')

{

if(this._x%26lt;left)

{

this._x=left;

}else

{

this._x = this._x + this._parent.boatSpeed;

}

}





on (keyPress ''%26lt;Right%26gt;'')

{

if(this._x%26gt;right)

{

this._x=right;

}else

{

this._x = this._x - this._parent.boatSpeed;

}

}



on (keyPress ''%26lt;Up%26gt;'') {

if(this._y%26lt;top)

{

this._y=top;

}else

{

this._y = this._y - this._parent.boatSpeed;

}

}



on (keyPress ''%26lt;Down%26gt;'') {

if(this._y%26gt;bottom)

{

this._y=bottom;

}else

{

this._y = this._y + this._parent.boatSpeed;

}

}




don't you have your left and right keys switched? and are
your buttons movieclip buttons? if so, use:



this._parent.left; // etc instead of left within your button
handlers.

No comments:

Post a Comment