遇到Flash Actionscript3的问题

时间:2017-03-22 13:09:13

标签: actionscript-3 flash

想知道你们是否可以帮助我。我使用在线教程创建了以下内容。

我正在制作一个小型闪存项目,而且我已经撞墙了。基本上,我有3个场景。一个主页,和2个游戏。我的主页和其中一个游戏在动作层中的时间轴上编程。第三场比赛适用于Main.as.问题是我想在Flappy游戏中应用一个名为home的按钮,但由于我已经将.as文件用于此代码,我不确定该怎么做。

基本上,我的问题是"如何向" Flappy"添加按钮?现场?每当我尝试这样做并进行测试时,它就会在场景1中播放Flappy游戏。

Main.as(" Flappy" Scene3)

    package{
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event; //used for ENTER_FRAME event
    //import flash.events.MouseEvent;

    public class Main extends MovieClip{

        //These are all of the constants used.

        const gravity:Number = 1.5;            //gravity of the game. How fast ball falls
        const dist_btw_obstacles:Number = 300; //distance between two obstacles
        const ob_speed:Number = 8;             //speed of the obstacle
        const jump_force:Number = 15;          //force with which it jumps

        //variables
        var player:Player = new Player();      
        var lastob:Obstacle = new Obstacle();  //varible to store the last obstacle in the obstacle array
        var obstacles:Array = new Array();     //an array to store all the obstacles
        var yspeed:Number = 0;                 //A variable representing the vertical speed of the bird
        var score:Number = 0;                  //A variable representing the score

        public function Main(){
            init();
        }

        function init():void {
            //initialize all the variables
            player = new Player();
            lastob = new Obstacle();
            obstacles = new Array();
            yspeed = 0;
            score = 0;

            //add player to center of the stage the stage
            player.x = stage.stageWidth/2;
            player.y = stage.stageHeight/2;
            addChild(player);

            //create 3 obstacles ()
            createObstacle();
            createObstacle();
            createObstacle();

            //Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
            addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
        }

        private function key_up(event:KeyboardEvent){
            if(event.keyCode == Keyboard.SPACE){
                //If space is pressed then make the bird
                yspeed = -jump_force;
            }
        }

        function restart(){
            if(contains(player))
                removeChild(player);
                for(var i:int = 0; i < obstacles.length; ++i){
                    if(contains(obstacles[i]) && obstacles[i] != null)
                    removeChild(obstacles[i]);
                    obstacles[i] = null;
                }
                obstacles.slice(0);
                init();
        }

        function onEnterFrameHandler(event:Event){
            //update player
            yspeed += gravity;
            player.y += yspeed;

            //restart if the player touches the ground
            if(player.y + player.height/2 > stage.stageHeight){
                restart();
            }

            //Don't allow the sheep to go above the screen
            if(player.y - player.height/2 < 0){
                player.y = player.height/2;
            }

            //update obstacles
            for(var i:int = 0;i<obstacles.length;++i){
                updateObstacle(i);
            }

            //display the score
            scoretxt.text = String(score);
        }

        //This functions update the obstacle
        function updateObstacle(i:int){
            var ob:Obstacle = obstacles[i];

            if(ob == null)
            return;
            ob.x -= ob_speed;

            if(ob.x < -ob.width){
                //if an obstacle reaches left of the stage then change its position to the back of the last obstacle
                changeObstacle(ob);
            }

            //If the bird hits an obstacle then restart the game
            if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
               || ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
               || ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
               || ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true)){
                restart();
            }

            //If the bird got through the obstacle without hitting it then increase the score
            if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered){
                ++score;
                ob.covered = true;
            }
        }

        //This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
        function changeObstacle(ob:Obstacle){
            ob.x = lastob.x + dist_btw_obstacles;
            ob.y = 100+Math.random()*(stage.stageHeight-200);
            lastob = ob;
            ob.covered = false;
        }

        //this function creates an obstacle
        function createObstacle(){
            var ob:Obstacle = new Obstacle();
            if(lastob.x == 0)
            ob.x = 800;
            else
            ob.x = lastob.x + dist_btw_obstacles;
            ob.y = 100+Math.random()*(stage.stageHeight-200);
            addChild(ob);
            obstacles.push(ob);
            lastob = ob;
        }  
    }
 }

这是我的主页的代码,名为&#34; Scenery&#34; (场景1)位于时间轴的动作层中。

        stop();

    import flash.events.MouseEvent;

    sun1.addEventListener(MouseEvent.CLICK,cloudy);

    function cloudy (e:MouseEvent){
        cloud1.x = cloud1.x + 10;
        cloud2.x = cloud2.x - 10;

    }

    pong_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
        gotoAndStop(1, "SheepyPong");
    }

    dodge_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
    function mouseDownHandler3(event:MouseEvent):void {
        gotoAndStop(1, "Flappy");
    }

This is my code for a very simple "SheepyPong" (Scene2) game which is linked to from front page:

var upPressed:Boolean = false;
var downPressed:Boolean = false;

var velocityX:Number = 5;
var velocityY:Number = 5;

movieClip_1.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
    if (upPressed)
    {
        movieClip_1.y -= 5;
    }
    if (downPressed)
    {
        movieClip_1.y += 5;
    }
    sheep.x += velocityX;
    sheep.y += velocityY;

    if(sheep.y + sheep.height/2 > stage.stageHeight || sheep.y - sheep.height/2 < 0){
        velocityY *= -1
    }

    if(sheep.hitTestObject(AI) || sheep.hitTestObject(movieClip_1)){
        velocityX *= -1;
    }

    if (sheep.x < 0) {
        score2.text = (int(score2.text) +1).toString();
        sheep.x = 275;
        sheep.y = 200;
        velocityX *= -1;
    }

    if (sheep.x > stage.stageWidth) {
        score1.text = (int(score1.text) +1).toString();
        sheep.x = 275;
        sheep.y = 200;
        velocityX *= -1;
    }


    AI.y = sheep.y;
}

function fl_SetKeyPressed(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = true;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = true;
            break;
        }

    }
}

function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP:
        {
            upPressed = false;
            break;
        }
        case Keyboard.DOWN:
        {
            downPressed = false;
            break;
        }

    }//end of switch
}//end of function 

home_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
    gotoAndStop(1, "Scenery");
}

我在运行时也会在输出中出现此错误。它实际上并没有影响outpu

TypeError:错误#1009:无法访问空对象引用的属性或方法。     在Main / onEnterFrameHandler()[/ Users / mynamehere / Documents / Game / Main.as:91]

//This is line 91 in the Main.as file:
scoretxt.text = String(score);

0 个答案:

没有答案