gpt4 book ai didi

actionscript-3 - 为什么我不能从其他类访问公共(public)变量?

转载 作者:行者123 更新时间:2023-12-04 06:43:18 25 4
gpt4 key购买 nike

我想在我的游戏文档类中定义我的变量,然后在我的 Movement 类中使用其中的一些变量。然而,当我在我的运动类中使用这些变量时,我收到大量编译错误,说它们都是未定义的。

因此我的问题是,为什么我的公共(public)变量没有传递给我的其他类?我通过 import game; 导入了这个类,所以这让我很困惑。我可能只是做错了,但非常感谢您的帮助。

时间线

addChild((new Movement));

游戏文档类

package 
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class game extends MovieClip
{
public var area1:Boolean = true;
public var area2:Boolean = false;
public var area3:Boolean = false;

public var player1:Boolean = true;

public var playerPosKeeper_mc:MovieClip = new mc_PlayerPosKeeper();

public var up_dpad:MovieClip = new dpad_Up();
public var down_dpad:MovieClip = new dpad_Down();
public var left_dpad:MovieClip = new dpad_Left();
public var right_dpad:MovieClip = new dpad_Right();
public var menu_dpad:MovieClip = new dpad_Menu();
public var run_dpad:MovieClip = new dpad_Menu();

public var barrierRoof1_game:MovieClip = new game_BarrierRoof();
public var barrierRoof2_game:MovieClip = new game_BarrierRoof();
public var barrierSide1_game:MovieClip = new game_BarrierSide();
public var barrierSide2_game:MovieClip = new game_BarrierSide();

public function game()
{
trace("SUCCESS | Constructed Game Class");

}
}
}

运动类

package 
{
import game;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TouchEvent;
import flash.net.dns.AAAARecord;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;


public class Movement extends MovieClip
{
var inMotion:Boolean = false;

public function Movement()
{
trace("SUCCESS | Constructed Movement Class");

addChild(playerPosKeeper_mc);
playerPosKeeper_mc.x = 384;
playerPosKeeper_mc.y = 46;

addChild(up_dpad);
up_dpad.x = 55;
up_dpad.y = 336;

addChild(down_dpad);
down_dpad.x = 57;
down_dpad.y = 432;

addChild(left_dpad);
left_dpad.x = 19;
left_dpad.y = 372;

addChild(right_dpad);
right_dpad.x = 118;
right_dpad.y = 372;

addChild(menu_dpad);
menu_dpad.x = 61;
menu_dpad.y = 377;

addChild(run_dpad);
run_dpad.x = 684;
run_dpad.y = 369;

addChild(barrierRoof1_game);
barrierRoof1_game.x = 0;
barrierRoof1_game.y = 0;

addChild(barrierRoof2_game);
barrierRoof2_game.x = 0;
barrierRoof2_game.y = 470;

addChild(barrierSide1_game);
barrierSide1_game.x = 0;
barrierSide1_game.y = 0;

addChild(barrierSide2_game);
barrierSide2_game.x = 790;
barrierSide2_game.y = 0;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

up_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, moveUpTouchBEGIN);
up_dpad.addEventListener(TouchEvent.TOUCH_END, moveUpTouchEND);

}
public function moveUpTouchBEGIN(e:TouchEvent):void
{
trace("Touch Began")
}

public function moveUpTouchEND(e:TouchEvent):void
{
trace("Touch Ended")
}
}

}

最佳答案

您感到困惑,因为“公共(public)”并不意味着“全局”。将变量声明为公共(public)变量意味着您希望向任何实体授予对它的访问权限,前提是该实体知道该变量的位置

让我们看看你有什么。

  • 顶部显示对象:舞台。
  • 主要时间轴:游戏是舞台的 child 。
  • MovieClips up_dpad、down_dpad等,都是游戏实例的字段。
  • 运动:实例是游戏的 child 。

在 Movement 中,您尝试访问 up_dpad 和其他内容,但它们被保留为游戏实例的字段,与 Movement 实例无关。这就是为什么它们不在 Movement 的寻址范围内。

因此,如果您想将这些变量声明为游戏字段,则需要以正确的方式从 Movement 中对它们进行寻址。

忘记时间表。在游戏中(出于缘故,除非在 Guzzle 下,否则永远不要用小写字母调用类),我相信这是一个根文档类:

    public function Game()
{
trace("SUCCESS | Constructed Game Class");

// Pass reference to this instance to the constructor.
var aMove:Movement = new Movement(this);

// Add it as a child.
addChild(aMove);
}

在运动中:

    public function Movement(main:Game)
{
trace("SUCCESS | Constructed Movement Class");

// Access variables inside Game instance via given reference.
addChild(main.playerPosKeeper_mc);
main.playerPosKeeper_mc.x = 384;
main.playerPosKeeper_mc.y = 46;

// ...and so on
}

这些是面向对象编程的基础知识。如果您想访问对象内部的任何内容,您需要先访问该对象。

关于actionscript-3 - 为什么我不能从其他类访问公共(public)变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com