"调用可能未定义的方法" - 大项目

时间:2015-10-28 07:01:25

标签: actionscript-3 actionscript

我在一个侧面滚动游戏项目,但我一直在努力与AS文件。首先,我没有在主时间线第一帧上编码,我使用外部作为整个项目的文件。

到目前为止,我有2个单独的文件,主要一个名为Tuan.as,另一个名为Enemy.as

我单独使用Tuan.as文件运行没有问题,但是当我决定添加更多功能时,比如使用另一个名为Enemy.as的文件添加敌人,我无法正确导入它们。我收到错误"调用可能未定义的方法addEnemytoLevel1"

这是我整个游戏的代码,第一个文件Tuan.as:

package  {

import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.events.*;
import flash.geom.*;
import flash.ui.*;
import flash.events.*;

public class Tuan extends MovieClip {


    public var myChannel:SoundChannel = new SoundChannel();
    public var mySound:Sound = new general();



    public var leftPressed:Boolean = false;
    public var rightPressed:Boolean = false;
    public var upPressed:Boolean = false;
    public var downPressed:Boolean = false;

    public var leftBumping:Boolean = false;
    public var rightBumping:Boolean = false;
    public var upBumping:Boolean = false;
    public var downBumping:Boolean = false;

    public var leftBumpPoint:Point = new Point(-30, -55);
    public var rightBumpPoint:Point = new Point(30, -55);
    public var upBumpPoint:Point = new Point(0, -120);
    public var downBumpPoint:Point = new Point(0, 0);

    public var scrollX:Number = 0;
    public var scrollY:Number = 500;

    public var xSpeed:Number = 0;
    public var ySpeed:Number = 0;

    public var speedConstant:Number = 4;
    public var frictionConstant:Number = 0.9;
    public var gravityConstant:Number = 1.8;
    public var jumpConstant:Number = -35;
    public var maxSpeedConstant:Number = 18;

    public var doubleJumpReady:Boolean = false;
    public var upReleasedInAir:Boolean = false;

    private var keyCollected:Boolean = false;
    private var keyCollected2:Boolean = false;
    private var keyCollected3:Boolean = false;
    private var keyCollected4:Boolean = false;
    private var keyCollected5:Boolean = false;
    private var keyCollected6:Boolean = false;
    private var keyCollected7:Boolean = false;
    private var keyCollected8:Boolean = false;

    var enemyList:Array = new Array();      
    public var doorOpen:Boolean = false;

     public var nakedboard:Boolean = false;
     public var currentLevel:int = 1;

    addEnemiesToLevel1();

    public function addEnemiesToLevel1():void{
    addEnemy(620, -115);
    addEnemy(900, -490);
    addEnemy(2005, -115);
    addEnemy(1225, -875);
    }

    public function Tuan() {


        myChannel = mySound.play(0, 9999);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

        stage.addEventListener(Event.ENTER_FRAME, loop);

    }


    public function loop(e:Event):void{
        if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
            //trace("leftBumping");
            leftBumping = true;
        } else {
            leftBumping = false;
        }

        if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
            //trace("rightBumping");
            rightBumping = true;
        } else {
            rightBumping = false;
        }

        if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
            //trace("upBumping");
            upBumping = true;
        } else {
            upBumping = false;
        }

        if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
            //trace("downBumping");
            downBumping = true;
        } else {
            downBumping = false;
        }   


        if(leftPressed){
            xSpeed -= speedConstant;
            player.scaleX = -1;

        } else if(rightPressed){
            xSpeed += speedConstant;
            player.scaleX = 1;
        }


        if(leftBumping){
            if(xSpeed < 0){
                xSpeed *= -0.5;
            }
        }

        if(rightBumping){
            if(xSpeed > 0){
                xSpeed *= -0.5;
            }
        }

        if(upBumping){
            if(ySpeed < 0){
                ySpeed *= -0.5;
            }
        }

        if(downBumping){ //if the character touches the floor
            if(ySpeed > 0){ 
                ySpeed = 0; //set the y speed to zero
            }
            if(upPressed){ //and if the up arrow is pressed
                ySpeed = jumpConstant; //set the y speed to the jump constant
            }

            //DOUBLE JUMP
            if(upReleasedInAir == true){
                upReleasedInAir = false;
            }
            if(doubleJumpReady == false){
                doubleJumpReady = true;
            }
        } else { //if we are not touching the floor

            ySpeed += gravityConstant; //accelerate downwards

            if(upPressed == false && upReleasedInAir == false){
                upReleasedInAir = true;
                //trace("upReleasedInAir");
            }
            if(doubleJumpReady && upReleasedInAir){
                if(upPressed){ //and if the up arrow is pressed
                    //trace("doubleJump!");
                    doubleJumpReady = false;
                    ySpeed = jumpConstant; //set the y speed to the jump constant
                }
            }

        }

        if(xSpeed > maxSpeedConstant){ //moving right
            xSpeed = maxSpeedConstant;
        } else if(xSpeed < (maxSpeedConstant * -1)){ //moving left
            xSpeed = (maxSpeedConstant * -1);
        }

        xSpeed *= frictionConstant;
        ySpeed *= frictionConstant;

        if(Math.abs(xSpeed) < 0.5){
            xSpeed = 0;
        }

        scrollX -= xSpeed;
        scrollY -= ySpeed;

        back.x = scrollX;
        back.y = scrollY;

        cave.x = scrollX * 0.2;
        cave.y = scrollY * 0.2;


if (nakedboard == false){
            if(player.hitTestObject(back.nakedboard)){
                back.nakedboard.gotoAndPlay(2);

            }
        }
        if(keyCollected == false ){
            if(player.hitTestObject(back.puzzle.redKey)){
                back.puzzle.redKey.visible = false;
                keyCollected2 = true;

                trace("need next key")

                }
            }
        if (keyCollected2 == true ){
            if(player.hitTestObject(back.puzzle.orangeKey)){
                back.puzzle.orangeKey.visible = false;
                keyCollected3 = true;

            } 
        }

        if(doorOpen == false){
            if(keyCollected3 == true){
                if(player.hitTestObject(back.puzzle.lockedDoor)){
                    back.puzzle.lockedDoor.gotoAndStop(2);
                    doorOpen = true;

                    trace("door open");
                }
            }
        }       


    }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    public function gotoLevel2():void{
        back.nakedboard.gotoAndStop(1);
        back.puzzle.gotoAndStop(2);
        back.visuals.gotoAndStop(2);
        back.collisions.gotoAndStop(2);
        scrollX = 0;
        scrollY = 500;
        back.puzzle.lockedDoor.gotoAndStop(1);
        /*back.other.doorKey1.visible = true;*/


    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public function gotoLevel3():void{

        back.puzzle.gotoAndStop(3);
        back.visuals.gotoAndStop(3);
        back.collisions.gotoAndStop(3);
        scrollX = 0;
        scrollY = 500;

    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public function nextLevel():void{
        currentLevel++;
        trace("Next Level: " + currentLevel);
        if(currentLevel == 2){
           gotoLevel2();
        }
        // can be extended...
         else if(currentLevel == 3) { 
             gotoLevel3(); 
             }  
    }

    public function addEnemy(xLocation:int, yLocation:int):void
{
    var enemy:Enemy = new Enemy(xLocation, yLocation);
    back.addChild(enemy);

    enemy.addEventListener(Event.REMOVED, enemyRemoved);
    enemyList.push(enemy);
}
    public function enemyRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED, enemyRemoved);
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}

    public function keyDownHandler(e:KeyboardEvent):void{
        if(e.keyCode == Keyboard.LEFT){
            leftPressed = true;

        } else if(e.keyCode == Keyboard.RIGHT){
            rightPressed = true;

        } else if(e.keyCode == Keyboard.UP){
            upPressed = true;

        } else if(e.keyCode == Keyboard.DOWN){
            downPressed = true;
            if(doorOpen && player.hitTestObject(back.puzzle.lockedDoor)){
                nextLevel();
                myChannel.stop();

            }
        }   
    }

    public function keyUpHandler(e:KeyboardEvent):void{
        if(e.keyCode == Keyboard.LEFT){
            leftPressed = false;

        } else if(e.keyCode == Keyboard.RIGHT){
            rightPressed = false;

        } else if(e.keyCode == Keyboard.UP){
            upPressed = false;

        } else if(e.keyCode == Keyboard.DOWN){
            downPressed = false;
        }   
    }
}

}

这是Enemy.as文件:

package{
import flash.display.MovieClip;
import flash.events.Event;

public class Enemy extends MovieClip{

    public function Enemy(xLocation:int, yLocation:int)
    {
        // constructor code
        x = xLocation;
        y = yLocation;

        addEventListener(Event.ENTER_FRAME, loop);
    }

    public function loop(e:Event):void
    {
        //the looping code goes here
    }

    public function removeSelf():void
    {
        trace("remove self");
        removeEventListener(Event.ENTER_FRAME, loop); //stop the loop
        this.parent.removeChild(this); //tell this object's "parent object" to remove this object
    }
}

}

如果我不想说非常糟糕,我不太擅长AS,我无法弄清楚如何让它发挥作用,非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我认为你的意思是addEnemiesToLevel1()(将敌人添加到1级)而不是addEnemytoLevel1(),你不能这样调用它,如果在你的内部使用它应该在你的类的另一个函数内调用,在您的情况下,您可以在构造函数中调用它:

public function Tuan()
{
    // ...

    addEnemiesToLevel1();
}

希望可以提供帮助。