简体   繁体   English

如何在AS3中访问另一个类

[英]How to access another class in AS3

Right, i've searched high and low for an answer to this, but no-one seems to have the same issue as me. 是的,我一直在寻找答案,但似乎没有人遇到与我相同的问题。 I've created 3 classes all of them movieclips. 我已经创建了3个类,所有这些类都包含动画片段。 2 of them are like buttons so when pressed I want the 3rd movieclip to move to the left or right but it comes up with error 1119: Access of possibly undefined property x through a reference with static type Class. 它们中的2个就像按钮,因此在按下时我希望第3个动画片段向左或向右移动,但出现错误1119:通过具有静态类型Class的引用访问可能未定义的属性x。 The 3 classes are all linked in the fla file so not sure why it's not as simple as just typing the class name to access it. 这3个类都链接到fla文件中,因此不确定为什么它不像键入类名那样简单。 Anyway here is the code: 无论如何,这里是代码:

First class (which I want to move using a mouse press): 头等舱(我想通过单击鼠标移动):

package code
{
import flash.display.MovieClip;

public class Walking extends MovieClip
{
    public function Walking()
    {
        x = 600;
        y = 350;

        gotoAndStop(1);
    }
}   

}

Second class: 第二类:

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;
    }
}







}

Third Class: (havn't coded yet) 第三类:(尚未编码)

package code
{
import flash.display.MovieClip;

public class Right extends MovieClip
{
    public function Right()
    {

    }
}







}

I would recommend you to keep your game logic in a single class and then reference the buttons. 我建议您将游戏逻辑放在一个类中,然后再参考按钮。 In this case you don't even need a class for the buttons (yet). 在这种情况下,您甚至不需要按钮类。 Something like this ... 像这样的东西...

//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;
}

Thanks for your help guys, I re-read some more on classes and it turns out I forgot about the document class. 感谢您的帮助,我重新阅读了有关类的更多信息,结果我忘记了文档类。 What I needed to do was to import the movieclips to stage using the document class (Thus creating instances of them, aka instantiation) and then I was able to access the x and y properties. 我需要做的是使用文档类将影片剪辑导入舞台(从而创建它们的实例,又称为实例化),然后可以访问x和y属性。 I was confused as to what a class was and it is just code outside of the document class and fla that links to an object like a movieclip and runs separately to the document class through all instances of the object. 我对类是什么感到困惑,它只是文档类和fla之外的代码,该代码链接到诸如动画片段之类的对象,并通过该对象的所有实例分别运行到文档类。 Anyway here is what I have now, working fine :) 无论如何,这就是我现在所拥有的,工作正常:)

Document Class: 文件类别:

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;
    }
}

}

Walking: 步行:

package code
{
import flash.display.MovieClip;

public class Walking extends MovieClip
{
    public function Walking()
    {
        gotoAndStop(1);
    }
}









}

Left: 剩下:

package code
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Left extends MovieClip
{
    public function Left()
    {

    }
}







}

Right: 对:

package code
{
import flash.display.MovieClip;

public class Right extends MovieClip
{
    public function Right()
    {

    }
}







}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM