Flash makes a simple second-hop object

Source: Internet
Author: User

It is very simple. You can press any key to perform a square jump, which can be a two-step hop or a light or heavy hop. It can be determined based on the length of the button. This is a question made at the Brotherhood forum. I should share it with you later ~

The code is as follows:

Package {
/* Author: CYPL */
Import flash. display .*;
Import flash. events .*;
Import flash. utils. Timer;
Public class JumpTest extends Sprite {
Private static const G: int = 1;
Private var box: Sprite;
Private var landObject: Sprite;
Private var ySpeed: int = 0;
Private var jumpSpeed: int =-15;
Private var keyPressed: Boolean = false;
Private var keyReleased: Boolean = true;
Private var comboJump: Boolean = false;
Private var inAir: Boolean = true;
Private var timer: Timer;
Public function JumpTest (){
Box = drawRect (stage. StageWidth-50)/2,100, 50, 0xff0000 );
LandObject = drawRect (0,300, stage. stageWidth, 20 );
(Timer = new Timer (30). start ();
ConfigureControl ();
                }
Private function configureControl (): void {
Stage. addEventListener (KeyboardEvent. KEY_DOWN, keyDownHandler, false, 0, true );
Stage. addEventListener (KeyboardEvent. KEY_UP, keyUpHandler, false, 0, true );
Timer. addEventListener (TimerEvent. TIMER, timerHandler, false, 0, true );

                }
Private function drawRect (posX: Number, posY: Number, w: Number, h: Number, color: uint = 0): Sprite {
Var rect: Sprite;
With (rect = new Sprite ){
Graphics. beginFill (color );
Graphics. drawRect (0, 0, w, h );
X = posX;
Y = posY;
                        }
Return Sprite (addChild (rect ));
                }
Private function keyDownHandler (evt: KeyboardEvent): void {
KeyReleased & inAir &&! ComboJump & (comboJump = true, ySpeed = jumpSpeed, trace ("comboJump "));
! KeyPressed & (keyPressed = true, inAir = true, ySpeed = jumpSpeed, timer. start ());
KeyReleased = false;
                }
Private function keyUpHandler (evt: KeyboardEvent): void {
KeyReleased = true;
YSpeed <-5 & (ySpeed + = 8) & ySpeed> 0 & ySpeed = 0;
                }
Private function timerHandler (evt: TimerEvent): void {
Evt. updateAfterEvent ();
YSpeed + = G;
Box. y + = ySpeed;
YSpeed> 0 & (box. hitTestObject (landObject) & (trace (landObject. y), box. y = landObject. y-box.height, timer. stop (), keyPressed = false, inAir = false, comboJump = false );
                }



        }
}

// Document class, set in fla to see the effect!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.