A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Movement Help

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    4

    Movement Help

    Heres a little background of what I have done. I have made a map, with roads that dont go in straight lines. There are curves, angles etc. To make an animation of my character moving along the road was easy using a GUIDELINE for the movieclip to follow.

    What I want to do is to be able to control that movement, with maybe something like keys. I have tried usign some KEY press functions to move the char using X and Y coordinates, BUT unfortunatelly the character moves off the guideline and is albe to go anywhere on the screen. I WANT to be able to use the keys to move the character BUT I want him to say on the guide line. Any suggests or ways to do something like this? Other ideas are also acceptable.

    Thanks in advance.

  2. #2
    Member
    Join Date
    Aug 2006
    Posts
    56
    Out of boredom I spent a little time toying with this, and eventually came up with this.

    Code:
    onClipEvent(enterFrame)	{
    
    	if (Key.isDown(Key.RIGHT))	{
    		this._x += charspeed;
    		while (_root.roads.hitTest(this._x,this._y,1) == 0)	{
    			this._x -= 1;	}	}
    		
    	if (Key.isDown(Key.LEFT))	{
    		this._x -= charspeed;
    		while (_root.roads.hitTest(this._x,this._y,1) == 0)	{
    			this._x += 1;	}	}
    			
    	if (Key.isDown(Key.UP))	{
    		this._y -= charspeed;
    		while (_root.roads.hitTest(this._x,this._y,1) == 0)	{
    			this._y += 1;	}	}
    			
    	if (Key.isDown(Key.DOWN))	{
    		this._y += charspeed;
    		while (_root.roads.hitTest(this._x,this._y,1) == 0)	{
    			this._y -= 1;	}	}
    		
    }
    Placed on the character movieclip, that should allow you to navigate it without exiting the boundaries of your roads, provided that you properly label said roads movie clip and assign the charspeed value. As long as the roads exceed 1 pixel in width it should function.

    I initially tried to have the character adjust itself appropriately when traveling on an angled or curving road, but I couldn't figure out a plausible code that worked for more than a single road.

    Hope that helps!

  3. #3
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    that code will only do motion vertical or horiziontal.

    Is that what you want, or were you looking more for a way to turn your character to face any direction?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center