FlashDevelop中AS3项目:将孩子从其他类不可见;键盘输入不动的孩子视觉孩子、不动、键盘输入、视觉

2023-09-08 15:10:04 作者:仰泳奈何桥

我开始是在FlashDevelop中为AS3项目完成100%的新项目。为了澄清,我不是创造的Adobe Flash的任何一点的FLA;的Adobe Flash将永远不会被使用。

在我的AS3项目的文档类,我将需要创建和引用都设置在单独的。作为类文件影片剪辑。该文档类将有EventListeners的是看键盘presses并在需要时将影片剪辑。通过键盘输入动作做工精细,当影片剪辑设置并增加新文档类的阶段。不过,我有当影片剪辑是由一个单独的类设置的问题。

除了我的项目我的工作,我创建了一个运行三个测试这个堆栈溢出问题示例项目。此示例项目包含三个文件都保存在同一文件夹:Main.as(文档类),createSquare.as(创建方MC),并createCircle.as(创建圆MC)

测试#1(蓝色矩形):在Main.as我创建了一个新的MC称为charRectangle。的大小,尺寸,颜色等在这里定义,然后将其添加到舞台。当函数keyIsDown()执行,在蓝色矩形移动向左或向右的舞台上正确。这是一个成功的测试。

测试#2(绿色广场):现在,我测试创建从一个单独的类文件中的影片剪辑。在Main.as我创建了一个名为charSquare新的变量,并把它定义为一个新的createSquare()。 createSquare.as定义的特性,并增加了孩子的阶段。问题:当项目测试没有绿色广场的舞台上可见;然而,跟踪输出显示的左,右箭头键其实都是更新charSquare的X位置。如何让我的影片剪辑节目?我的解决办法是在...

测试#3(黄圈):我开始一切都以同样的方式对像我一样的绿色广场上的黄色圆圈。我们称这个charCircle。在Main.as时定义变量作为一个新的charCircle()我传递下去本在createCircle.as功能的话。这又告诉函数createCircle增加孩子这个,舞台上的父MC。现在,我们可以看到一个明显的形状。问题:跟踪输出显示左,右箭头键更新的X位置,但黄圈不动。如何让我这个电影剪辑举动?

主要目标:我想避免,说明在Main.as的字符形状的所有属性,并保持了孤立于其他类文件。在Main.as我想只要创建一个新的变量,把它定义为一个新的createSquare()/ createCircle(),然后使用键盘功能Main.as来处理运动。我以后会继续键盘控制在一个名为keyboardControls.as第四个测试类。

我可以在Adobe Flash中的FLA开始的时候,说明文档类,在舞台上创造MC符号,给他们一个实例名称,然后在工作中的FlashDevelop的AS文件解决这些问题。但同样,这个项目需要,我忽略的Adobe Flash / FLA所以这不是我期待的解决方案。我需要去创造​​100%的动态中的FlashDevelop AS3。

感谢您的任何和所有帮助。该人士$ ​​C $ C三个测试文件在下面找到。

Main.as

 包{
    进口的flash.display.MovieClip;
    进口对象类型:flash.events.Event;
    进口flash.events.KeyboardEvent;

    公共类主要扩展影片剪辑{

        私人VAR charRectangle:影片剪辑;
        私人VAR charSquare:createSquare;
        私人VAR charCircle:createCircle;

        公共函数main(){

            跟踪(功能:主()已启动);

            //创建矩形字符就在这里
            charRectangle =新的MovieClip();
            charRectangle.graphics.beginFill(0x0000FF);
            charRectangle.graphics.drawRect(200,20,250,100);
            charRectangle.graphics.endFill();
            stage.addChild(charRectangle);

            //创建一个从类方形字符
            charSquare =新createSquare();

            //创建一个从类方形字符
            charCircle =新createCircle(本);

            //事件监听用户的输入
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
        }

        私有函数keyIsDown(E:的KeyboardEvent):无效{
            //左箭头键操作
            如果(e.key code == 37){
                charRectangle.x  -  = 5;
                charSquare.x  -  = 5;
                charCircle.x  -  = 5;
            }
            //右箭头键操作
            如果(e.key code == 39){
                charRectangle.x + = 5;
                charSquare.x + = 5;
                charCircle.x + = 5;
            }
            追踪(charRectangle.x =+ charRectangle.x +,charSquare.x =+ charSquare.x +,charCircle.x =+ charCircle.x);
        }

    }

}
 
FlashDevelop最实用配置法

createSquare.as

 包{

    进口的flash.display.MovieClip;

    公共类createSquare扩展影片剪辑{

        私人VAR方:影片剪辑;

        公共职能createSquare(){

            跟踪(功能:createSquare()已经启动。);

            方=新的MovieClip();
            square.graphics.beginFill(为0xFF0000);
            square.graphics.drawRect(200140100100);
            square.graphics.endFill();
            的addChild(广场);

        }

    }

}
 

createCircle.as

 包{

    进口的flash.display.MovieClip;

    公共类createCircle扩展影片剪辑{

        私人VAR圈:影片剪辑;

        公共职能createCircle(parentMC:影片剪辑){

            跟踪(功能:createCircle()已经启动。);

            圈=新的MovieClip();
            circle.graphics.beginFill(0x00FF00);
            circle.graphics.drawCircle(200,350,75);
            circle.graphics.endFill();
            parentMC.addChild(圈);

        }
    }

}
 

解决方案

我猜你应该从动作显示列表中发挥更习惯于操作显示对象(的addChild / removeChild之/嵌套的/ etc。)

您创建 charRectangle 键,您添加到舞台,所以你可以看到它。 初始化 charCircle 像这样

  charCircle =新createCircle(本);
 

和在内部将其添加到主(构造函数中通过),两个以上的剪辑也会显示。这不是 charSquare ,你做创建(因此可以访问它,你可以在你的痕迹看)的情况下,但你不添加到显示列表。 试试这个:

  //创建一个从类方形字符
charSquare =新createSquare();
//将其添加到显示列表
的addChild(charSquare)
 

另外,你提到你想将所有的剪辑一次,你可以做,如果你只是窝所有3人到一个单独的Sprite / MovieClip的:

 包{
    进口flash.display.Sprite;
    进口的flash.display.MovieClip;
    进口对象类型:flash.events.Event;
    进口flash.events.KeyboardEvent;
    进口flash.ui.Keyboard;

    公共类主要扩展影片剪辑{

        私人VAR字符:雪碧;

        公共函数main(){

            跟踪(功能:主()已启动);

            字符=新的Sprite();
            的addChild(焦);

            //创建矩形字符就在这里
            VAR charRectangle:影片剪辑=新的MovieClip();
            charRectangle.graphics.beginFill(0x0000FF);
            charRectangle.graphics.drawRect(200,20,250,100);
            charRectangle.graphics.endFill();
            char.addChild(charRectangle);

            //创建一个从类方形字符
            VAR charSquare:影片剪辑=新createSquare();
            char.addChild(charSquare);
            //创建一个从类方形字符
            charCircle =新createCircle(焦);

            //事件监听用户的输入
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
        }

        私有函数keyIsDown(E:的KeyboardEvent):无效{
            如果(e.key code == Keyboard.LEFT)char.x  -  = 5;
            如果(e.key code == Keyboard.RIGHT)char.x + = 5;
            跟踪(字符位置,char.x,char.y);
        }

    }

}
 

(我没有测试上面的code,所以有可能是语法错误,但应具体说明这个思想明确)。

两个对方注意事项:

影片剪辑是一个动态类,它比简单的类象的雪碧。除非你使用一个时间表(和你的项目并不像它会),我建议使用雪碧代替。对于一个简单的项目也不会影响性能很多,但是从长远来看(对于更复杂的项目),它的安全,以节省资源。

我想你createCircle和createSquare课程将会改变,变得更加复杂,但如果他们要保持像现在这样,他们可以更简单:

包{

 进口flash.display.Sprite;

公共类广场扩展Sprite {


    公共职能广场(){

        跟踪(本);

        graphics.beginFill(为0xFF0000);
        graphics.drawRect(200140100100);
        graphics.endFill();

    }

}
 

}

如果不需要在createSquare或嵌套元素createCircle,您可以使用的图形这是很基本的,虽然你可能会发现雪碧更加灵活。另请注意,我命名的类方形。这是一个惯例,开始一个类的名称以大写字母(如雪碧/影片剪辑/等),以小写字母的属性和方法(例如:X /阿尔法/等),常量全部大写(如KEY_DOWN,单击,等)等。 本公约,createSquare听起来更像是一个比一个类的函数名。 你没有义务遵循的惯例,但它可能更容易为其他人轻松地完成code。在未来的扫描。其主要思想是要一致。

I am starting a new project that is done 100% in FlashDevelop as an AS3 Project. To clarify, I am not creating an FLA in Adobe Flash at any point; Adobe Flash will never be used.

In the document class of my AS3 project I will need to create and reference movieclips that are set up in separate .as class files. The document class will have eventlisteners that watch for keyboard presses and move the movieclips when required. Movements via the keyboard inputs work fine when the movieclip is set up and added to the stage from the document class. However, I have problems when the movieclip is set up from a separate class.

Apart from my project I am working on, I have created a sample project that runs three tests for this Stack Overflow question. This sample project contains three files all stored in the same folder: Main.as (the document class), createSquare.as (creates square MC), and createCircle.as (creates circle MC).

Test #1 (Blue Rectangle): In Main.as I create a new MC called charRectangle. The size, dimensions, color, etc are defined here and then it is added to the stage. When the function keyIsDown() executes, the blue rectangle moves left or right on the stage correctly. This is a succesful test.

Test #2 (Green Square): Now I test creating a movieclip from a separate class file. In Main.as I create a new variable called charSquare and define it as a new createSquare(). createSquare.as defines the characteristics and adds the child to the stage. Problem: When the project is tested no green square is visible on the stage; however, trace output shows the the left and right arrow keys are in fact updating the X position of charSquare. How do I make the movieclip show? My solution to that is in...

Test #3 (Yellow Circle): I start everything the same way for the yellow circle as I did for the green square. We name this one charCircle. In Main.as when defining the variable as a new charCircle() I pass along the word "this" to the function in createCircle.as. This in turn tells the function createCircle to add the child to "this", the parent MC on the stage. Now we can see a visible shape. Problem: Trace output shows the left and right arrow keys are updating the X position, but the yellow circle does not move. How do I make this movie clip move?

Primary Goal: I want to avoid stating all the attributes of the character shapes in Main.as and keep that isolated to other class files. In Main.as I want to just create a new variable, define it as a new createSquare()/createCircle(),and then use a keyboard function in Main.as to handle movements. I would later keep keyboard controls in a fourth testing class called keyboardControls.as.

I can solve these problems when starting from an FLA in Adobe Flash, stating the document class, creating MC symbols on the stage, giving them an instance name, and then working on the AS files in FlashDevelop. But again, this project requires that I omit Adobe Flash/FLA so this is not the solution i am looking for.. I need to create it 100% dynamic in FlashDevelop AS3.

Thank you for any and all assistance. The source code for the three test files are found below.

Main.as

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

    public class Main extends MovieClip {

        private var charRectangle:MovieClip;
        private var charSquare:createSquare;
        private var charCircle:createCircle;

        public function Main() {

            trace("Function: Main() has started.");

            // Create rectangle character right here
            charRectangle = new MovieClip();
            charRectangle.graphics.beginFill(0x0000FF);
            charRectangle.graphics.drawRect(200,20,250,100);
            charRectangle.graphics.endFill();
            stage.addChild(charRectangle);

            // Create square character from class
            charSquare = new createSquare();

            // Create square character from class
            charCircle = new createCircle(this);

            // Event listener for user input
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
        }

        private function keyIsDown(e:KeyboardEvent):void {
            // Left arrow key actions
            if (e.keyCode == 37) {
                charRectangle.x -= 5;
                charSquare.x -= 5;
                charCircle.x -= 5;
            }
            // Right arrow key actions
            if (e.keyCode == 39) {
                charRectangle.x += 5;
                charSquare.x += 5;
                charCircle.x += 5;
            }
            trace("charRectangle.x = " + charRectangle.x + ", charSquare.x = " + charSquare.x + ", charCircle.x = " + charCircle.x);
        }

    }

}

createSquare.as

package {

    import flash.display.MovieClip;

    public class createSquare extends MovieClip {

        private var square:MovieClip;

        public function createSquare() {

            trace("Function: createSquare() has started.");

            square = new MovieClip();
            square.graphics.beginFill(0xFF0000);
            square.graphics.drawRect(200,140,100,100);
            square.graphics.endFill();
            addChild(square);

        }

    }

}

createCircle.as

package {

    import flash.display.MovieClip;

    public class createCircle extends MovieClip {

        private var circle:MovieClip;

        public function createCircle(parentMC:MovieClip) {

            trace("Function: createCircle() has started.");

            circle = new MovieClip();
            circle.graphics.beginFill(0x00FF00);
            circle.graphics.drawCircle(200,350,75);
            circle.graphics.endFill();
            parentMC.addChild(circle);

        }
    }

}

解决方案

I'm guessing you should play more with the display list from actionscript to get used to manipulated display objects (addChild/removeChild/nesting/etc.)

You create charRectangle and you add that to the stage, so you can see it. You initialize charCircle like so

charCircle = new createCircle(this);

and internally you add it to Main(passed in the constructor), both of the clips above are also displayed. That's not the case for charSquare which you do create (and can therefore access it as you can see in your traces), but you do not add to the display list. Try this:

// Create square character from class
charSquare = new createSquare();
// add it to the display list
addChild(charSquare)

Also, you mentioned that you'd like to move all of the clips at once, which you can do if you simply nest all 3 of them into a separate Sprite/MovieClip:

package {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class Main extends MovieClip {

        private var char:Sprite;

        public function Main() {

            trace("Function: Main() has started.");

            char = new Sprite();
            addChild(char);

            // Create rectangle character right here
            var charRectangle:MovieClip = new MovieClip();
            charRectangle.graphics.beginFill(0x0000FF);
            charRectangle.graphics.drawRect(200,20,250,100);
            charRectangle.graphics.endFill();
            char.addChild(charRectangle);

            // Create square character from class
            var charSquare:MovieClip = new createSquare();
            char.addChild(charSquare);
            // Create square character from class
            charCircle = new createCircle(char);

            // Event listener for user input
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
        }

        private function keyIsDown(e:KeyboardEvent):void {
            if(e.keyCode == Keyboard.LEFT)  char.x -= 5;
            if(e.keyCode == Keyboard.RIGHT) char.x += 5;
            trace("char position",char.x,char.y);
        }

    }

}

(I haven't tested the above code, so there might be syntax errors, but it should illustrate the idea clearly).

Two other side notes:

MovieClip is a dynamic class which is slower than simpler classes like Sprite. Unless you're using a timeline (and your project doesn't look like it will), I recommend using Sprite instead. For a simple project it won't impact performance a lot, but on the long run(for more complex projects), it's safer to save resources.

I imagine your createCircle and createSquare classes will change and become more complex, but if they are to remain as they are now, they could be even simpler:

package {

import flash.display.Sprite;

public class Square extends Sprite {


    public function Square() {

        trace(this);

        graphics.beginFill(0xFF0000);
        graphics.drawRect(200,140,100,100);
        graphics.endFill();

    }

}

}

If don't need to nest elements in createSquare or createCircle, you can use the Shape which is really basic, although you might find Sprite more flexible. Also note that I've named the class Square. It's a convention to start a class' name with an uppercase letter (e.g. Sprite/MovieClip/etc.), properties and methods with a lowercase letter (e.g. x/alpha/etc.), constants all in caps (e.g. KEY_DOWN, CLICK, etc.) and so on. By this convention, createSquare sounds more like a function name than a class. You're not obliged to follow the convention, but it might make it easier for other people to easily scan through your code in the future. The main idea is to be consistent.