To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-11-2004, 04:30 PM   #1
garrybibson
Member
 
Join Date: Jul 2002
Posts: 72
restricting movement from arrow keypad

Hi,

I am playing around making a little game from a tutorial and i am having trouble trying to limit where the "car" can travel on the screen. At present it has a free range and will disappear out of view if asked to. Can anyone make a suggestion what code i need to add? I have been searching and trying for two hours but just cant get it to work.

This is what i have at present

code:

listener = new Object();
listener.onKeyDown = function () {
delete car.onEnterFrame;
var yval = 0;
var xval = 0;
switch (Key.getCode()) {
case Key.DOWN: yval = 8; car.rotate(30,30); break;
case Key.UP: yval = -8; car.rotate(-30,30); break;
case Key.LEFT: xval = -8; car.rotate(-30,30);break;
case Key.RIGHT: xval = 8; car.rotate(30,30);
}
car.onEnterFrame = function(){
this._y += yval;
this._x += xval;

}
};



Basically i just want to limit the cars movement to within the track!
Any help or advice would be greatly welcomed.

Barry

EDIT: Added as tags. - jbum

Last edited by jbum; 10-11-2004 at 04:44 PM.
garrybibson is offline   Reply With Quote
Old 10-11-2004, 04:51 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Here are two methods you can use:

Method #1: You can add bounds to your onEnterFrame handler, as follows:

code:


// Adjust these numbers to taste
minX = 10;
maxX = Stage.width-10;
minY = 10;
maxY = Stage.height-10;

car.onEnterFrame = function()
{
this._y = Math.min(Math.max(minY, this._y + yval), maxY);
this._x = Math.min(Math.max(minX, this._x + xval), maxX);
}




This will keep the car within a rectangle specified by minX-maxX and minY-maxY.


Method #2: If you want to restrict the car to a more complicated shape, then you'll probably want to do a hitTest on the track, like so:

For example:

code:

car.onEnterFrame = function()
{
var nx = this._x + yval;
var ny = this._y + xval;
if (_root.track.hitTest(nx,ny,true))
{
this._x = nx;
this._y = ny;
}
}

__________________
jbum is offline   Reply With Quote
Old 10-11-2004, 05:09 PM   #3
garrybibson
Member
 
Join Date: Jul 2002
Posts: 72
i want a rectangle shape so the first bit of code is fine.

Thank you very much, people like you are priceless!

Barry
garrybibson is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:22 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.