当我按下按钮时,我的游戏滞后

时间:2014-10-18 23:17:27

标签: actionscript-3 air flash-builder flash-cs5 flashdevelop

我有一个应用程序,我正在使用AS3为iOS和Android开发。我目前在这个项目中遇到了几个性能问题。这是我的第一款应用和游戏。因此,非常感谢任何类型的反馈或见解。我的游戏是一个越野车自行车前轮游戏。它允许用户按下一个按钮(gasPress)向上转动,另一个按钮(制动器)向下转动。按下“gasPress”按钮后,布尔“holdGas”设置为true。在我的“gameLoop”(Enter_Frame事件)中,自行车向上旋转,IF“holdGas”等于true。按下制动器的代码是相同的,除了它旋转自行车。我的建筑物在屏幕上滑动,造成自行车移动的错觉。构建影片剪辑缓存为位图。我使用相同构建影片剪辑的两个实例。一旦第一个剪辑从舞台上滑下,它就会被放在另一个剪辑的后面。在一个回收的机制lol。我遇到的问题是,当我按下“gasPress”按钮时,它在前轮/向上旋转之前滞后。当它滞后时,在后面移动的建筑物会跳过。它每次都会发生。因此,如果你继续点击gasPress(这是游戏玩法的一部分),屏幕上的对象会继续跳跃。它使游戏无法播放。我已经通过完全移除代码来测试按钮,以向上/向下移动自行车,游戏仍然会跳过。当气体被按下并且自行车向上旋转时,我注意到更多的跳跃或滞后。我目前的帧速率设置为32,它在24时也是如此。

public function TheGame() {
            //Connect to tapjoy's ad network
            //tjClass = new TapJoyClass();
            //stage.scaleMode = StageScaleMode.NO_SCALE;
            //stage.align = StageAlign.TOP_LEFT; 
            //Give game focus so the keyboard keys will work on computer
            stage.focus = this;
            globalStage = this.stage;
            //Checks if ScoreData and Bike data shared object has been created, if not it creates
            scoreInfo = SharedObject.getLocal ( "ScoreData" );
            bikeInfo = SharedObject.getLocal ( "BikeData" );
            //scoreInfo.clear ();
            //bikeInfo.clear ();
            mostSprocketsCollected = scoreInfo.data.mostSprocketsCollected;
            //Handling  background
            background1 = new Background;
            background2 = new Background;
            addChild ( background1 );
            addChild ( background2 );
            background1.cacheAsBitmap = true;
            background2.cacheAsBitmap = true;
            background1.mouseChildren = false;
            background1.mouseEnabled = false;
            background2.mouseChildren = false;
            background2.mouseEnabled = false;
            background1.x = background1.width;
            background2.x = background1.x + background2.width;
            //Handling buildings
            building1 = new Buildings;
            building2 = new Buildings;
            addChild ( building1 );
            addChild ( building2 );
            building1.cacheAsBitmap = true;
            building2.cacheAsBitmap = true;
            building1.mouseChildren = false;
            building1.mouseEnabled = false;
            building2.mouseChildren = false;
            building2.mouseEnabled = false;
            building1.x = building1.width;
            building2.x = building1.x + building2.width;
            //Choose which bike will be used
            if( bikeInfo.data.EquippedBike == undefined ){
                //Start off with original bike
                whichBike = "OriginalDirtBike";
                bikeInfo.data.BikeColor = "OriginalDirtBike";
            }else{
                whichBike = bikeInfo.data.EquippedBike;
            }
            if( whichBike == "Banshee" ){
                //Add far wheels first
                wheelClip2 = new Banshee_Far_WheelClip;
                addChild ( wheelClip2 );
                //wheelClip2. = 208.30;
                wheelClip2.y = 350;
                //Add Banshee
                bitmapBike = new Banshee_Bitmap ();
                bitmapBike.gotoAndStop ( bikeInfo.data.BikeColor );
                addChild ( bitmapBike );
                bitmapBike.y = 399.80;
                bitmapBike.x = 0;
                //Add frontWheels
                wheelClip = new Banshee_WheelClip ();
                wheelClip.backTire.frontFace.gotoAndStop ( bikeInfo.data.BikeColor );
                wheelClip.frontTire.frontFace.gotoAndStop ( bikeInfo.data.BikeColor );
                addChild ( wheelClip );
                wheelClip.x = 187.70;
                wheelClip.y = 399;
            }else if( whichBike == "SupermotoDRZ400" ){
                //Add Wheels
                wheelClip = new WheelClip ();
                wheelClip.backTire.gotoAndStop ( bikeInfo.data.BikeColor );
                wheelClip.frontTire.gotoAndStop ( bikeInfo.data.BikeColor );
                addChild ( wheelClip );
                wheelClip.x = 0;
                wheelClip.y = 409.45;
                //Add bike
                bitmapBike = new SuperMoto_Bitmap ();
                bitmapBike.gotoAndStop ( bikeInfo.data.BikeColor );
                addChild ( bitmapBike );
                bitmapBike.y = 399.80;
            }else if( whichBike == "OriginalDirtBike" ){
                //Add wheels
                wheelClip = new OriginalBike_WheelClip ();
                wheelClip.backTire.gotoAndStop ( bikeInfo.data.BikeColor );
                wheelClip.frontTire.gotoAndStop ( bikeInfo.data.BikeColor );
                addChild ( wheelClip );
                wheelClip.x = 0;
                wheelClip.y = 405;
                //Add Bike
                bitmapBike = new OriginalBike_Bitmap ();
                addChild ( bitmapBike );
                bitmapBike.gotoAndStop ( bikeInfo.data.BikeColor );
                bitmapBike.y = 399.80;
                //Add rider
                //rider = new Rider_Original_bitmap ();
                //addChild ( rider );
                //rider.y = 353.1
            }
            //bitmapBike.mouseChildren = false;
            //bitmapBike.mouseEnabled = false;
            //bitmapBike.cacheAsBitmapMatrix = bitmapBike.transform.concatenatedMatrix;
            //bitmapBike.cacheAsBitmap = true;
            //
            trace ( "bitmap bike : " + getSize ( bitmapBike) );

            //rider.cacheAsBitmapMatrix = rider.transform.concatenatedMatrix;
            //rider.cacheAsBitmap = true;
            rider.mouseChildren = false;
            rider.mouseEnabled = false;

            gasPress.addEventListener ( MouseEvent.MOUSE_DOWN, hitGas );
            gasPress.addEventListener ( MouseEvent.MOUSE_UP, releaseGas );
            brake.addEventListener ( MouseEvent.MOUSE_DOWN, hitBrake );
            brake.addEventListener ( MouseEvent.MOUSE_UP, releaseBrake );
            stage.addEventListener ( KeyboardEvent.KEY_DOWN, useKeyboard);
            stage.addEventListener ( KeyboardEvent.KEY_UP, stopUseKeyboard);
            addEventListener ( Event.ADDED_TO_STAGE, init );
            ScoreBoardFormat = new TextFormat ();
            ScoreBoardFormat.bold = true; 
            sprocketVector = new Vector.< MovieClip > ();
            sprocketPool = new SprocketPool( Sprocket, pool_SprocketAmount );
            stage.addEventListener ( Event.RESIZE, resizeObjects );
            stage.dispatchEvent( new Event( Event.RESIZE ) );
            if( Accelerometer.isSupported ){
                //on mobile device
            }else{
                //on desktop
                gravity = 15; // low values for phone
                dy = 60; // low values for phone
                backgroundDx = -160;
                buildingDx  = -300;
                dx  = 300; // speed and direction
            }
            //start bike off in a wheelie
            bitmapBike.rotation = -45;
            rider.rotation = -45;
            wheelClip.x = -15;
            rider.x = bitmapBike.x;
            //Add the first sprocket
            addSprocket();
        }

    //Following Functions are Gas and Brake related
        public function hitGas (e:MouseEvent){
            e.stopImmediatePropagation();
            holdGas = true;
            pressedBrake = "no";
        }
        public function hitBrake (e:MouseEvent){
            e.stopImmediatePropagation();
            pressedBrake = "yes"; 
        }
        public function releaseGas (e:MouseEvent){
            e.stopImmediatePropagation();
            holdGas = false;
        }
        public function releaseBrake (e:MouseEvent){
            e.stopImmediatePropagation();
            pressedBrake = "no"; 
        }




    //Game Loop
        public function gameLoop ( e:Event ):void{
            e.stopImmediatePropagation();
            var timePassed:int = getTimer()-lastTime;
            lastTime += timePassed;

            if ( whichBike == "SupermotoDRZ400" ){
                 //Keep wheels in position with the bike
                 wheelClip.rotation = bitmapBike.rotation;
                 wheelClip.x = bitmapBike.x;
                 //make tires spin
                 wheelClip.frontTire.rotation += 75*timePassed/1000;
                 wheelClip.backTire.rotation += 75*timePassed/1000;
                 rider.rotation = bitmapBike.rotation - 1;
                 rider.x = bitmapBike.x + 25;
            }else if ( whichBike == "OriginalDirtBike" ){
                //Keep wheels in position with the bike
                wheelClip.rotation = bitmapBike.rotation - 1.5;
                wheelClip.rotation = bitmapBike.rotation;
                wheelClip.x = bitmapBike.x + 16.5;
                //make tires spin
                wheelClip.frontTire.rotation += 75*timePassed/1000;
                wheelClip.backTire.rotation += 75*timePassed/1000;
                //rider
                rider.x = bitmapBike.x;
                rider.rotation = bitmapBike.rotation;
            }else if ( whichBike == "Banshee" ){
                rider.rotation = bitmapBike.rotation;
                rider.x = bitmapBike.x;
                //First wheels
                wheelClip.x = bitmapBike.x;
                wheelClip.rotation = bitmapBike.rotation;
                wheelClip.frontTire.frontFace.rotation += 75*timePassed/1000;
                wheelClip.frontTire.backFace.rotation  += 75*timePassed/1000;
                wheelClip.backTire.frontFace.rotation += 75*timePassed/1000;
                wheelClip.backTire.backFace.rotation  += 75*timePassed/1000;
                //second wheels
                wheelClip2.x = bitmapBike.x;
                wheelClip2.rotation = bitmapBike.rotation;
                wheelClip2.farBackTire.frontFace.rotation += 75*timePassed/1000;
                wheelClip2.farFrontTire.frontFace.rotation += 75*timePassed/1000;
                wheelClip2.farFrontTire.backFace.rotation += 75*timePassed/1000;
            }

            if( popUp != null ){
                this.setChildIndex ( popUp, this.numChildren - 2 );
            }
            this.setChildIndex ( rider, this.numChildren - 1 );
            //Stores the bikes current rotation point
            var currRotation = bitmapBike.rotation;

            //make shadow follow bike
            var bikeX:int = bitmapBike.x;
            newShadow.x = bikeX + 150; //109.8;
            //ANITMATING THE DRIVER!!!!
            if( currRotation <= -11 ){ //This should occur at a lower
                rider.gotoAndPlay ( "sitDown" );
                sitDown = true;
                newShadow.gotoAndStop ( "fullShadow" );
            }
            if( currRotation <= -15 ){
                newShadow.gotoAndStop( "three4thShadow" );
                leanBack = true;
            }
            //just change shadow with this one
            if( currRotation <= -25 ){
                newShadow.gotoAndStop ( "midShadow" );
            }
            if( currRotation <= -30 ){
                rider.gotoAndPlay ( "LeanBack" );
                leanBack = true;
                newShadow.gotoAndStop ( "smallShadow" );
            }
            if( currRotation <= -34 ){ //Highest
                rider.gotoAndPlay ( "LeanBack2" );
                leanBack = true;
                newShadow.gotoAndStop ( "smallestShadow" );
            }
            if( currRotation <= -35 ){ //Highest
                rider.gotoAndPlay ( "LeanBack2" );
                leanBack = true;
                newShadow.gotoAndStop ( "allTheWayBack" );
            }
            //if player holds or hit the brake
            if( pressedBrake == "yes" ) { //for some reason the opposite is working right now
                var currBikePosition:int;
                currBikePosition = bitmapBike.rotation;
                bitmapBike.rotation += 2;
                currBikePosition = 0;
            }
            //gravity pulling bike down
            bitmapBike.rotation += gravity*timePassed/1000;
            bitmapBike.rotation += gravity*timePassed/1000;
            //if player holding gas, do wheeliez
            if( holdGas == true ){
                //Make bike wheelie
                bitmapBike.rotation -= dy*timePassed/1000;
                rider.rotation -= dy*timePassed/1000;
            }
            //Move Bike to center, then move buildings
            if( bitmapBike.x <=187 ){
                //move bike
                bitmapBike.x += dx*timePassed/1000;
            }else{
                //move buildings and background
                building1.x += buildingDx*timePassed/1000;
                building2.x += buildingDx*timePassed/1000;
                //buildings
                if( building1.x <= -25 ){
                    building1.x = building2.x + building1.width;
                }else if( building2.x <= -25 ){
                    building2.x = building1.x + building2.width;
                }
                background1.x += backgroundDx*timePassed/1000;
                background2.x += backgroundDx*timePassed/1000;
                //background
                if( background1.x <=0 ){
                    background1.x = background2.x + background1.width;
                }else if ( background2.x <=0 ){
                    background2.x = background1.x + background2.width;
                }
            }

2 个答案:

答案 0 :(得分:0)

你是如何处理&#34;背景图像&#34;?图像有多大?

最有可能的原因是,当您的自行车移动时,您必须重新绘制所有内容,因为它无法再重复使用缓存位图的旧值。而且由于你的背景很大,这会导致你注意到的性能下降。

首先,您应该在&#34;巨大的移动背景上定义 scrollRect &#34;而不是仅仅绘制整个背景。这导致仅显示图像的一部分被发送到GPU。例如bg.scrollRect = new Rectangle(offsetX,offsetY, 1280, 1024);

阅读http://gskinner.com/blog/archives/2006/11/understanding_d.html,了解scrollRect的简短介绍。

CacheAsBitmap可能没有按照您的意愿执行操作,如果您要更改每个帧上的显示对象的外观,您实际上会丢失性能,因为您首先必须绘制项目,然后将它们放入位图,并且然后绘制该位图。然后在下一帧,你将最终重做完全相同的事情(因为旧的缓存位图不再有效)。当复杂的矢量形状对于一定数量的帧保持相同时,cacheAsBitmap非常有用。

有关更全面的优化指南,请阅读以下两个大量的pdf文件: http://help.adobe.com/en_US/as3/mobile/flashplatform_optimizing_content.pdf http://help.adobe.com/en_US/as3/dev/as3_devguide.pdf

答案 1 :(得分:0)

在“发布”设置中,将“硬件加速”设置为GPU,将从处理器中取出