A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: make character move and jump

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    11

    make character move and jump

    1st POST...
    hi, i'm trying to create a game (duh) but i'm already hitting some problems rite at the start. I'm just starting to learn flash so forgive me. I'll first show you the code below.

    Code:
    onClipEvent (load) 
    	{startspeed = 10; 
    	gravity = 10.8; 
    	jumping = 0;
    	speed = 6;
    	}
    onClipEvent (enterFrame)
    {
    	if (this.speed < 0)
    	{this.gotoAndStop("still");
    	}
    	else if(Key.isDown(Key.RIGHT))
    	{this._x += speed;
    	this.gotoAndStop("right");
    	}
    	else if(Key.isDown(Key.LEFT))
    	{this._x -= speed;
    	this.gotoAndStop("left");
    	}
    	else if(Key.isDown(Key.UP)and jumping==0)
    	{jumping = 1; 
    	starttime = getTimer()/1000; 
    	startposition = _y;
    	this.gotoAndStop("jump")
    	}
    	if (jumping==1) { 
    	time = getTimer()/1000-starttime; 
    	_y = _y-(startspeed-gravity*time); 
    	if (_y>startposition) { 
    	_y = startposition; 
    	jumping = 0; 
    	} 
    	} 
                 else {this.gotoAndStop("still")
                 }
    }
    i'm just testing out the code first so this ain't the real thing. this code is on a movieclip called "ball". within the ball clip i have 3 layers, one for code one for labels and one to change the look of the clip when the arrow keys are pressed. the jumping code i got from somewhere else and tried to adapt it to suit me.
    On the root scene i have two layers one for the ball and one with code that is "jumping = 0". Now the ball move when i use the arrows and it also jumps but when you press an arrow key and then nothing else the look will stay the same(e.g. if i press right and then leave it it will stay on the frame that make it look going right)
    The problem it that it doesn't go back to it's original look of being still when no other keys are pressed. I just added the bottom else bit to show you the code for it to go back to original state. If i don't have this code the ball will change appearence but if i have this code the it will stay like a circle but when you jump and press left or right it changes.

    +Hopefully u understood this
    *is there any easier way to code it to jump
    *If anyone could please help me i woule be very greatful
    *if you didn't understand anything please tell me
    THANK YOU

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Code:
    onClipEvent (load) 
    	{startspeed = 10; 
    	gravity = 10.8; 
    	jumping = 0;
    	speed = 6;
    	}
    onClipEvent (enterFrame)
    {
    	if (this.speed < 0)
    	{this.gotoAndStop("still");
    	} else if (Key.isDown(Key.RIGHT))
    	{this._x += speed;
    	this.gotoAndStop("right");
    	} else if (Key.isDown(Key.LEFT))
    	{this._x -= speed;
    	this.gotoAndStop("left");
    	} else if (Key.isDown(Key.UP)and jumping==0)
    	{jumping = 1; 
    	starttime = getTimer()/1000; 
    	startposition = _y;
    	this.gotoAndStop("jump")
    	} else {
                 this.gotoAndStop("still")
            }
    	if (jumping==1) { 
    	     time = getTimer()/1000-starttime; 
    	     _y = _y-(startspeed-gravity*time); 
    	     if (_y>startposition) { 
    	          _y = startposition; 
    	          jumping = 0; 
    	     } 
    	} 
    }
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    11

    duh

    Thank you. such a simple answer.

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