Flash AS3播放器动画片段和创建实例的问题

时间:2016-05-26 01:16:04

标签: actionscript-3 flash

  

所以继续我的问题,我在Adobe Animate(Warmage.as)中创建了一个movieclip类,并将其添加到我的舞台(addChild(char))。我试图访问属性说明未定义的char属性。但是我为Warmage创建了一个类并创建了它的一个实例(char)。

package  
{

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;

public class Main_class extends MovieClip
{
    //player stats
    var hsp:Number = 0;
    var vsp:Number = 0;
    var floor:Number = 1318;
    var attackCounter = 5;
    var doubleJumpCount = 0;

    //Player states
    var rightSide:Boolean = false;       
    var rDown:Boolean = false;
    var lDown:Boolean = false;
    var jumped:Boolean = false;
    var onGround:Boolean = false;
    var crouchMode:Boolean = false;
    var attackMode:Boolean = false;
    var canDoubleJump = false;

    public function Main_class()
    {
         var char:Warmage = new Warmage();//Adds player to the level
         char.x = 500;
         char.y = 300;
         addChild(char);

         stage.addEventListener(Event.ENTER_FRAME, gameLoop);//Stage listens no matter what
         stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
         stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    }
    function gameLoop(e:Event):void
    {
        if(rDown)
          {
             char.x += 10;
          }
        if(lDown)
          {
             char.x -= 10; 
          }
    }
    function keyPressed(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = true;  
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = true;
          }
        if(e.keyCode == Keyboard.UP && onGround)
          {
             jumped = true;
             //doubleJumpCount += 1;
          }
        if(e.keyCode == Keyboard.DOWN && onGround)
          {
             crouchMode = true; 
          }
        if(e.keyCode == Keyboard.SPACE && onGround)
          {
              attackMode = false;
          }
    }
    function keyReleased(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = false; 
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = false;  
          }
        if(e.keyCode == Keyboard.UP)
          {
             jumped = true;  
          }
        if(e.keyCode == Keyboard.DOWN)
          {
             crouchMode = false; 
          }
        if(e.keyCode == Keyboard.SPACE)
          {
             attackMode = true;
          }
    }

}
}

1 个答案:

答案 0 :(得分:4)

因为您正在使用本地变量。

public function Main_class()
{
    var char:Warmage = new Warmage();//Adds player to the level

    trace(char);    // OK. You can available char inside of this function.
}

function gameLoop(e:Event):void
{
    trace(char);    // You can not available that variable here.
}

使用全局变量。在函数外声明变量。

private var char:Warmage;

public function Main_class()
{
    char = new Warmage();//Adds player to the level
    char.x = 500;
    char.y = 300;
    addChild(char);

    trace(char);    // OK
}

function gameLoop(e:Event):void
{
    trace(char);    // OK
}

请阅读"了解变量范围"
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html