论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash教程
Tag:flash cs,flash 8,鼠绘,as,手绘,工具,文字,loading,入门,初学,mc,影片剪辑,声音,mtv,游戏,引导,遮罩,菜单,补间,广告条,时钟,视频教程

简单的物体二段跳效果

文章类别:Flash | 发表日期:2008-9-21 19:52:20

  非常非常简单的效果,按任意键进行方块的跳跃,有二段跳效果和轻跳和重跳效果,根据按键长短判定的。这是在兄弟会论坛做的一道题,很应该转来跟大家分享下~

/UploadTeach/200804/20080409141318564.swf

代码如下:
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,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);
                }



        }
}
//文档类,fla中设置下就能看到效果

进入论坛和作者讨论学习:http://space.flash8.net/bbs/thread-352690-1-1.html
视频教程列表
文章教程搜索
 
Flash推荐教程
Flash热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058