从班级中将孩子添加到场景中

发布于 2024-09-01 07:14:02 字数 4995 浏览 11 评论 0原文

我对 Flash 不太熟悉,一直在编写一个程序,其中包含两个扩展 MovieClip 的类(Stems 和 Star)。

当用户停止拖动 Star 对象时,我需要创建一个新的 Stems 对象作为场景的子对象,但不知道如何从 Star 类的代码中引用场景。

我尝试将场景传递到 Star 的构造函数中并执行以下操作:

this.scene.addChild (new Stems ());

但显然这不是如何做到的...下面是 Stems 和 Stars 的代码,任何建议将不胜感激。

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


public class Stems extends MovieClip {
    public const centreX=1026/2;
    public const centreY=600/2;
    public var isFlowing:Boolean;
    public var flowerType:Number;
    public const outerLimit=210;
    public const innerLimit=100;

    public function Stems(fType:Number) {
        this.isFlowing=false;
        this.scaleX=this.scaleY= .0007* distanceFromCentre(this.x, this.y);
        this.setXY();
        trace(distanceFromCentre(this.x, this.y));
        if (fType==2) {
            gotoAndStop("Aplant");
        }

    }

    public function distanceFromCentre(X:Number, Y:Number):int {
        return (Math.sqrt((X-centreX)*(X-centreX)+(Y-centreY)*(Y-centreY)));
    }

    public function rotateAwayFromCentre():void {
        var theX:int=centreX-this.x;
        var theY:int = (centreY - this.y) * -1;
        var angle = Math.atan(theY/theX)/(Math.PI/180);
        if (theX<0) {
            angle+=180;
        }
        if (theX>=0&&theY<0) {
            angle+=360;
        }
        this.rotation = ((angle*-1) + 90)+180;

    }

    public function setXY() {
        do {
            var tempX=Math.random()*centreX*2;
            var tempY=Math.random()*centreY*2;
        } while (distanceFromCentre (tempX, tempY)>this.outerLimit || 
                distanceFromCentre (tempX, tempY)<this.innerLimit);
        this.x=tempX;
        this.y=tempY;
        rotateAwayFromCentre();
    }

    public function getFlowerType():Number {
        return this.flowerType;
    }
}

}

包裹 { 导入 flash.display.MovieClip; 导入 flash.events.*; 导入 flash.utils.Timer;

public class Star extends MovieClip {
    public const sWide=1026;
    public const sTall=600;
    public var startingX:Number;
    public var startingY:Number;
    public var starColor:Number;
    public var flicker:Timer;
    public var canUpdatePos:Boolean=true;
    public const innerLimit=280;

    public function Star(color:Number, basefl:Number, factorial:Number) {
        this.setXY();
        this.starColor=color;
        this.flicker = new Timer (basefl + factorial * (Math.ceil(100* Math.random ())));
        this.flicker.addEventListener(TimerEvent.TIMER, this.tick);

        this.addEventListener(MouseEvent.MOUSE_OVER, this.hover);
        this.addEventListener(MouseEvent.MOUSE_UP, this.drop);
        this.addEventListener(MouseEvent.MOUSE_DOWN, this.drag);

        this.addChild (new Stems (2));

        this.flicker.start();
        this.updateAnimation(0, false);
    }

    public function distanceOK(X:Number, Y:Number):Boolean {
        if (Math.sqrt((X-(sWide/2))*(X-(sWide/2))+(Y-(sTall/2))*(Y-(sTall/2)))>innerLimit) {
            return true;
        } else {
            return false;

        }
    }

    public function setXY() {
        do {
            var tempX=this.x=Math.random()*sWide;
            var tempY=this.y=Math.random()*sTall;
        } while (distanceOK (tempX, tempY)==false);
        this.startingX=tempX;
        this.startingY=tempY;
    }

    public function tick(event:TimerEvent) {
        if (this.canUpdatePos) {
            this.setXY();
        }
        this.updateAnimation(0, false);
        this.updateAnimation(this.starColor, false);
    }

    public function updateAnimation(color:Number, bright:Boolean) {

        var brightStr:String;

        if (bright) {
            brightStr="bright";
        } else {
            brightStr="low";

        }
        switch (color) {
            case 0 :
                this.gotoAndStop("none");
                break;
            case 1 :
                this.gotoAndStop("N" + brightStr);
                break;
            case 2 :
                this.gotoAndStop("A" + brightStr);
                break;
            case 3 :
                this.gotoAndStop("F" + brightStr);
                break;
            case 4 :
                this.gotoAndStop("E" + brightStr);
                break;
            case 5 :
                this.gotoAndStop("S" + brightStr);
                break;
        }
    }

    public function hover(event:MouseEvent):void {
        this.updateAnimation(this.starColor, true);
        this.canUpdatePos=false;
    }

    public function drop(event:MouseEvent):void {
        this.stopDrag();
        this.x=this.startingX;
        this.y=this.startingY;
        this.updateAnimation(0, false);
        this.canUpdatePos=true;



    }

    public function drag(event:MouseEvent):void {
        this.startDrag(false);
        this.canUpdatePos=false;

    }

}

}

I'm new to flash in general and have been writing a program with two classes that extend MovieClip (Stems and Star).

I need to create a new Stems object as a child of the scene when the user stops dragging a Star object, but do not know how to reference the scene from within the Star class's code.

I've tried passing the scene into the constructor of the Star and doing sometihng like:

this.scene.addChild (new Stems ());

But apparently that's not how to do it... Below is the code for Stems and Stars, any advice would be appreciated greatly.

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


public class Stems extends MovieClip {
    public const centreX=1026/2;
    public const centreY=600/2;
    public var isFlowing:Boolean;
    public var flowerType:Number;
    public const outerLimit=210;
    public const innerLimit=100;

    public function Stems(fType:Number) {
        this.isFlowing=false;
        this.scaleX=this.scaleY= .0007* distanceFromCentre(this.x, this.y);
        this.setXY();
        trace(distanceFromCentre(this.x, this.y));
        if (fType==2) {
            gotoAndStop("Aplant");
        }

    }

    public function distanceFromCentre(X:Number, Y:Number):int {
        return (Math.sqrt((X-centreX)*(X-centreX)+(Y-centreY)*(Y-centreY)));
    }

    public function rotateAwayFromCentre():void {
        var theX:int=centreX-this.x;
        var theY:int = (centreY - this.y) * -1;
        var angle = Math.atan(theY/theX)/(Math.PI/180);
        if (theX<0) {
            angle+=180;
        }
        if (theX>=0&&theY<0) {
            angle+=360;
        }
        this.rotation = ((angle*-1) + 90)+180;

    }

    public function setXY() {
        do {
            var tempX=Math.random()*centreX*2;
            var tempY=Math.random()*centreY*2;
        } while (distanceFromCentre (tempX, tempY)>this.outerLimit || 
                distanceFromCentre (tempX, tempY)<this.innerLimit);
        this.x=tempX;
        this.y=tempY;
        rotateAwayFromCentre();
    }

    public function getFlowerType():Number {
        return this.flowerType;
    }
}

}

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

public class Star extends MovieClip {
    public const sWide=1026;
    public const sTall=600;
    public var startingX:Number;
    public var startingY:Number;
    public var starColor:Number;
    public var flicker:Timer;
    public var canUpdatePos:Boolean=true;
    public const innerLimit=280;

    public function Star(color:Number, basefl:Number, factorial:Number) {
        this.setXY();
        this.starColor=color;
        this.flicker = new Timer (basefl + factorial * (Math.ceil(100* Math.random ())));
        this.flicker.addEventListener(TimerEvent.TIMER, this.tick);

        this.addEventListener(MouseEvent.MOUSE_OVER, this.hover);
        this.addEventListener(MouseEvent.MOUSE_UP, this.drop);
        this.addEventListener(MouseEvent.MOUSE_DOWN, this.drag);

        this.addChild (new Stems (2));

        this.flicker.start();
        this.updateAnimation(0, false);
    }

    public function distanceOK(X:Number, Y:Number):Boolean {
        if (Math.sqrt((X-(sWide/2))*(X-(sWide/2))+(Y-(sTall/2))*(Y-(sTall/2)))>innerLimit) {
            return true;
        } else {
            return false;

        }
    }

    public function setXY() {
        do {
            var tempX=this.x=Math.random()*sWide;
            var tempY=this.y=Math.random()*sTall;
        } while (distanceOK (tempX, tempY)==false);
        this.startingX=tempX;
        this.startingY=tempY;
    }

    public function tick(event:TimerEvent) {
        if (this.canUpdatePos) {
            this.setXY();
        }
        this.updateAnimation(0, false);
        this.updateAnimation(this.starColor, false);
    }

    public function updateAnimation(color:Number, bright:Boolean) {

        var brightStr:String;

        if (bright) {
            brightStr="bright";
        } else {
            brightStr="low";

        }
        switch (color) {
            case 0 :
                this.gotoAndStop("none");
                break;
            case 1 :
                this.gotoAndStop("N" + brightStr);
                break;
            case 2 :
                this.gotoAndStop("A" + brightStr);
                break;
            case 3 :
                this.gotoAndStop("F" + brightStr);
                break;
            case 4 :
                this.gotoAndStop("E" + brightStr);
                break;
            case 5 :
                this.gotoAndStop("S" + brightStr);
                break;
        }
    }

    public function hover(event:MouseEvent):void {
        this.updateAnimation(this.starColor, true);
        this.canUpdatePos=false;
    }

    public function drop(event:MouseEvent):void {
        this.stopDrag();
        this.x=this.startingX;
        this.y=this.startingY;
        this.updateAnimation(0, false);
        this.canUpdatePos=true;



    }

    public function drag(event:MouseEvent):void {
        this.startDrag(false);
        this.canUpdatePos=false;

    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2024-09-08 07:14:02

最快的方法是使用引用 DisplayObject 父级的父级变量。

var stem:Stems = new Stems(2);
stem.x = x; //optional: set stem coordinates to that of Star
stem.y = y; 
parent.addChild(stem);

如果您想在每次星形拖动操作停止时将 Stems 对象添加到舞台,则需要将上述代码放入您的 drop 函数中。

The fastest way would be to use the parent variable which references the DisplayObject parent.

var stem:Stems = new Stems(2);
stem.x = x; //optional: set stem coordinates to that of Star
stem.y = y; 
parent.addChild(stem);

If you want to add the Stems object to the stage every time a star-drag action stops you need to put the above code inside your drop function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文