是的,我已经高低搜索了这个答案,但似乎没有人和我有同样的问题。我创建了3个类,所有这些都是动画片段。其中2个像按钮,所以按下时我希望第3个动画片段向左或向右移动但是它出现错误1119:通过静态类型Class的引用访问可能未定义的属性x。这三个类都链接在fla文件中,因此不确定为什么它不像输入类名来访问它那么简单。无论如何这里是代码:
头等舱(我想用鼠标按下移动):
package code
{
import flash.display.MovieClip;
public class Walking extends MovieClip
{
public function Walking()
{
x = 600;
y = 350;
gotoAndStop(1);
}
}
}
第二课:
package code
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Left extends MovieClip
{
public function Left()
{
addEventListener(MouseEvent.MOUSE_DOWN, moveleft);
}
protected function moveleft(event:MouseEvent):void
{
Walking.x += -10;
}
}
}
第三类:(已经编码)
package code
{
import flash.display.MovieClip;
public class Right extends MovieClip
{
public function Right()
{
}
}
}
答案 0 :(得分:0)
我建议你将游戏逻辑保存在一个类中,然后引用按钮。在这种情况下,您甚至不需要按钮类(尚未)。 像这样......
//Game.as
var arrowLeft : MovieClip;
var arrowRight : MovieClip;
var walker : MovieClip;
function Game()
{
arrowLeft.addEventListener(MouseEvent.CLICK, OnClickLeft);
arrowRight.addEventListener(MouseEvent.CLICK, OnClickRight);
}
function OnClickLeft(pEvent : MouseEvent) : void
{
Walk(-speed);
}
function OnClickRight(pEvent : MouseEvent) : void
{
Walk(speed);
}
function Walk(pSpeed : Number) : void
{
walker.x += pSpeed;
}
答案 1 :(得分:0)
感谢您的帮助,我重新阅读了更多关于课程的内容,结果我忘记了文档课程。我需要做的是使用文档类将影片剪辑导入舞台(从而创建它们的实例,也就是实例化),然后我就能够访问x和y属性。我对类是什么感到困惑,它只是文档类之外的代码和fla,它链接到像movieclip这样的对象,并通过对象的所有实例单独运行到文档类。无论如何,这就是我现在所做的,工作得很好:)。
文件类:
package {
import flash.display.MovieClip;
import code.Walking;
import code.Left;
import code.Right;
import flash.events.MouseEvent;
public class gameWalk extends MovieClip {
public var walker:Walking;
public var left:Left;
public var right:Right;
public function gameWalk()
{
walker = new Walking();
left = new Left();
right = new Right();
addChild(walker);
addChild(left);
addChild(right);
walker.x = 640;
walker.y = 360;
left.x = 65;
left.y = 660;
right.x = 1200;
right.y = 660;
createListeners()
}
public function createListeners():void
{
left.addEventListener(MouseEvent.CLICK, moveLeft);
right.addEventListener(MouseEvent.CLICK, moveRight);
}
public function moveLeft(event:MouseEvent):void
{
walker.x += -10;
}
public function moveRight(event:MouseEvent):void
{
walker.x += 10;
}
}
}
散步:
package code
{
import flash.display.MovieClip;
public class Walking extends MovieClip
{
public function Walking()
{
gotoAndStop(1);
}
}
}
左:
package code
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Left extends MovieClip
{
public function Left()
{
}
}
}
右:
package code
{
import flash.display.MovieClip;
public class Right extends MovieClip
{
public function Right()
{
}
}
}