在AS3中制造障碍

时间:2015-06-15 13:06:20

标签: actionscript-3 flash

我在Flash中制造障碍时遇到了麻烦。我设法以某种方式创造障碍,但只有在没有背景的情况下才能看到。添加一个时,它们就会消失......

这是main.as文件的代码:

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.filters.BitmapFilterQuality; 
import flash.filters.BlurFilter;
import flash.utils.setInterval;
import flash.utils.clearInterval;

public class Main extends MovieClip {

    private var obstacleArray:Array;
    private var obstacleInterval:uint;

    private var rural2:Rural2;
    private var rural3:Rural2;
    private var cityFront2:CityFront2;
    private var cityFront3:CityFront2;
    private var cityBack2:CityBack2;
    private var cityBack3:CityBack2;
    private var skyline:Skyline;
    private var skyline2:Skyline;
    public var ship:Ship;   



    public function Main() {
        // constructor code

        obstacleArray = new Array();
        obstacleInterval = setInterval(createObstacle, 1000);

        addSkylineToStage();
        addCityBack2ToStage();
        addCityFront2ToStage();
        addRural2ToStage();
        createShip();


        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }


    private function addSkylineToStage() {

    skyline = new Skyline();
    /*var myBlurFilter:BlurFilter = new BlurFilter(8,8);
    skyline.filters = [myBlurFilter];*/ 
    stage.addChild(skyline);

    skyline2 = new Skyline();
    skyline2.x = skyline.width;
    stage.addChild(skyline2);   

    }

    private function addCityBack2ToStage() {

    cityBack2 = new CityBack2();
    cityBack2.y = 310;
    cityBack2.x = 20;

    var myBlurFilter:BlurFilter = new BlurFilter(3.7,3.7);
    cityBack2.filters = [myBlurFilter];
    stage.addChild(cityBack2);

    cityBack3 = new CityBack2();
    cityBack3.y = 310;
    cityBack3.x = cityBack2.width-20;
    cityBack3.filters = [myBlurFilter]; 
    stage.addChild(cityBack3);  

    }

    private function addCityFront2ToStage() {

    cityFront2 = new CityFront2();
    cityFront2.y = 310;

    var myBlurFilter:BlurFilter = new BlurFilter(2.3,2.3);
    cityFront2.filters = [myBlurFilter];    
    stage.addChild(cityFront2);

    cityFront3 = new CityFront2();
    cityFront3.y = 310;
    cityFront3.x = cityFront2.width-8;
    cityFront3.filters = [myBlurFilter];    
    stage.addChild(cityFront3); 

    }

    private function addRural2ToStage() {

    rural2 = new Rural2();
    rural2.y = 410;
    stage.addChild(rural2); 

    rural3 = new Rural2();
    rural3.y = 410;
    rural3.x = rural2.width-2;
    stage.addChild(rural3); 

    }

    var position:String = 'bottom';

    private function createObstacle(){

        if(position == 'bottom'){
            position = 'top';
        }else{
            position = 'bottom';
        }

        var obstacle:Obstacle = new Obstacle(stage,position);
        obstacleArray.push(obstacle);
        obstacle.x = stage.stageWidth;
        addChild(obstacle);
    }

    private function stopObstacles(){
        for(var i:int = 0; i<obstacleArray.length;i++){

            var obstacle:Obstacle = obstacleArray[i];
            obstacle.removeEvents();
        }

        clearInterval(obstacleInterval);
    } 


    private function onEnterFrame(evt:Event) {

        rural2.x -= 2;
        rural3.x -= 2;

        if(rural2.x <= -rural2.width){
            rural2.x = rural3.width-4;

        }

        if(rural3.x <= -rural3.width){
            rural3.x = rural2.width-4;

        }

        cityFront2.x -= 1;
        cityFront3.x -= 1;

        if(cityFront2.x <= -cityFront2.width){
            cityFront2.x = cityFront3.width-15;

        }

        if(cityFront3.x <= -cityFront3.width){
            cityFront3.x = cityFront2.width-15;

        }

        cityBack2.x -= 0.5;
        cityBack3.x -= 0.5;

        if(cityBack2.x <= -cityBack2.width){
            cityBack2.x = cityBack3.width-50;

        }

        if(cityBack3.x <= -cityBack3.width){
            cityBack3.x = cityBack2.width-50;

        }

        skyline.x -= 0.25;
        skyline2.x -= 0.25;

        if(skyline.x <= -skyline.width){
            skyline.x = skyline2.width-2;

        }

        if(skyline2.x <= -skyline2.width){
            skyline2.x = skyline.width-2;

        }

        for(var i:int = 0; i<obstacleArray.length; i++){

            var obstacle:Obstacle = obstacleArray[i];
            if(obstacle.hitTestObject(ship)){

                stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                stopObstacles();

            }
        }
    }


    public function createShip() {

        ship = new Ship(stage);
        stage.addChild(ship);

        ship.x = 20;
        ship.y = 180;


    }

}

}

barrier.as文件的代码:

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;


public class Obstacle extends MovieClip {

    private var speed:Number = 3;
    private var mPosition:String;
    private var mStage:Stage;       

    public function Obstacle(stage:Stage, position:String = 'bottom') {
        // constructor code

        mPosition = position;
        mStage = stage;
        addEvents();
        setPosition();
    }

        private function setPosition(){

        var factor:Number = Math.random()+0.3;          
        this.scaleY = factor;

        if(mPosition == 'bottom'){

            this.y = mStage.stageHeight - this.height;

        }else{
            this.y = 0;
        }

    }

    private function addEvents(){

        addEventListener(Event.ENTER_FRAME, onFrame);

    }

    public function removeEvents(){

        removeEventListener(Event.ENTER_FRAME, onFrame);

    }

    private function onFrame(evt:Event){

        this.x -= speed;

    }

}

}

如何调整代码,以便在舞台上看到视差背景和障碍?

谢谢!

1 个答案:

答案 0 :(得分:0)

哦,最初我错过了代码中的“setInterval”,这确实会增加你对其他元素的障碍。也许尝试在添加时将其索引设置为顶部?所以:

addChildAt(障碍物,stage.numChildren-1)

为了测试分层问题,您可以尝试将所有元素alpha设置为0.5,这样您就可以看到某些东西是在舞台上但是在其他元素后面还是没有被添加到舞台上。

此外,http://www.monsterdebugger.com对于调试显示列表问题非常有用,因为您可以在调试器中选择元素并在屏幕上查看它们的位置。