A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [CS3, AS2] Simple movement with Acceleration

  1. #1
    Member
    Join Date
    May 2006
    Posts
    31

    [CS3, AS2] Simple movement with Acceleration

    I'm working on making a platformer in Flash and so far have an okay script for making my character speed up to a maximum walking speed quickly, so the movement isn't jerky. The only problem I have is once the variable of the walking speed gets all the way up, it stays this way. If I press the left or right key again, the movement speed is still at 5 so it's jerky and I really want to find a way to have smooth acceleration when moving in my game. I'm trying to implement a way to check if the key is down, so I can set the speed back to 0 if a key is released, but I'm not really sure how. This is my code so far. I'm not sure if I need variables for my left and right speed, but I thought it might come in handy in the future.

    Code:
    onClipEvent (load)
    {
    	rightSpeed=0;
    	leftSpeed=0;
    }
    
    onClipEvent (enterFrame)
    {
    	if (Key.isDown(Key.RIGHT)) 
    	{
    		rightSpeed++;
    		if (rightSpeed > 5)
    		{
    			rightSpeed=5;
    		}
    		this._x+=rightSpeed;
    	}
    
    	if (Key.isDown(Key.LEFT)) 
    	{
    		leftSpeed++;
    		if (leftSpeed > 5)
    		{
    			leftSpeed=5;
    		}
    		this._x-=leftSpeed;
    	}	
    }
    To rephrase my question, how could I do an event like Key.isUp? I know this doesn't exist, except with key listeners but I can't figure out how to use a listener. If someone could help me that would be great.

    Thanks
    Ethan

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    KeyUp exists as an event. You don't want to test for it onenterframe like you're doing with Key.isDown (which probably isn't the best approach either), but you can assign anything you want to it's handler (including another onenterframe to reduce rightSpeed or leftSpeed for example)
    PHP Code:
    Key.addListener({onKeyUp:function(){
        
    trace(Key.getCode());
    }}); 

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