How to create graphics when using classes? AS3

♀尐吖头ヾ 提交于 2019-12-12 02:59:45

问题


I am creating a "game" of sorts where three different colored circles appear on the screen and take different amounts of clicks to make disappear. I have a main mxml function and then 4 other classes. I thought I was close to finishing it, but I have encountered an error and it is not telling me what it is. Here is a link to the rubric here The error I get when I run the code is: "Error #2032: Stream Error. URL: file:///C|/Users/Gabe/Adobe Flash Builder 4.6/Project 1/bin-debug/framework_4.6.0.23201.swf" Here is my code:

main.mxml:

import com.multiClicker.*;

        import spark.components.Image;

        //create the init function
        public function init():void {
            //create new target and add it to the stage
            onEnterFrame();

        }

        public function onEnterFrame(e:Event):void{
            //2% chance of being added per frame
            if(Math.random() <= .02) {
                //33% chance of adding red target
                if(Math.random() <= .033){
                    //make a new image
                    var newCircle1:RedTarget = new RedTarget();

                    this.addElement(newCircle1);

                    //position circle 
                    newCircle1.x = Math.random() * stage.stageWidth;
                    newCircle1.y = Math.random() * stage.stageHeight;
                }
                //33% chance of adding blue target
                else if(Math.random() > .066){
                    //make a new image
                    var newCircle2:BlueTarget = new BlueTarget();

                    this.addElement(newCircle2);

                    //position circle 
                    newCircle2.x = Math.random() * stage.stageWidth;
                    newCircle2.y = Math.random() * stage.stageHeight;
                }
                //33% chance of adding green target
                else {
                    //make a new image
                    var newCircle3:GreenTarget = new GreenTarget();

                    this.addElement(newCircle3);

                    //position circle 
                    newCircle3.x = Math.random() * stage.stageWidth;
                    newCircle3.y = Math.random() * stage.stageHeight;
                }

            }
        }

my Target.as file:

package com.multiClicker{

//import the needed classes
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.utils.Timer;

import spark.components.Image;

public var hp:Number;

public class Target extends Image 
{

    public function Target() {

        //add event listeners
        this.addEventListener(MouseEvent.CLICK, onClick);

    }

    //sets the hp of the target
    public function hP():Number { 
        return hp;
    }

    //get function that returns false if alpha is <= 0
    public function dead():Boolean {

        if(alpha <= 0){
            return false;
        }

    }

    //subtracts one from targets HP when clicked
    public function onClick(e:MouseEvent = null):void {


        //subtracts one from hp each click
        hp--;

        if(hp <=0) {
            this.addEventListener(onEnterFrame);
        }

    }

    //subtracts .1 from the classes alpha
    public function onEnterFrame(e:Timer):void{
        this.alpha =- .1;
    }

    //draws the target
    public function drawTarget(color):void {

        var circle:Shape = new Shape();

        circle.graphics.beginFill(color);
        circle.graphics.drawCircle(0,0,30);

    }

}

}

then the target files Red, Blue, and GreenTarget.as which are all the same:

package com.multiClicker{   
import flash.events.MouseEvent;

public class RedTarget extends Target
{

    private var hp:Number = 3;

    public function RedTarget()
    {
        //draw the red circle
        this.graphics.beginFill(0x990000);
        this.graphics.drawCircle(0,0,30);

        //include the super function
        super();
    }

    //subtracts one from targets HP when clicked
    override public function onClick(e:MouseEvent=null):void {

        super.onClick(e);

        //subtracts one from hp each click
        hp--;

        if(hp <=0) {
            this.addEventListener(onEnterFrame);
        }

    }

}

}

I am getting an error in Target and GreenTarget only. It is not telling me what the error is or where. any help would be awesome!

edit: I posted the error message I was getting. I used the clean function on Flash Builder and it erased my errors. Then I go to run it again and I get a file not found error. It says my Main.swf cannot be found.

edit2: I think I found the error, I deleted my onEnterFrame function in my Main.mxml file and the errors went away. Now I just do not see what is wrong with that function.


回答1:


I made something similar using MiniBuilder:

http://www.swfcabin.com/open/1360282440

Here are the classes:

Application.as

package com.multiClicker {
import flash.display.*;
import flash.events.*;

public class Application extends Sprite {
    public function
    Application() {
        addEventListener( Event.ENTER_FRAME, onEnterFrame );
    }

    private function
    onEnterFrame( e:Event ):void {
        var circle:Target;
        var n:Number = Math.random();

        // 33% of 2% chance each of being added per frame
        if ( n <= 0.00667 )     circle = new RedTarget();
        else if( n <= 0.01334 ) circle = new GreenTarget();
        else if( n <= 0.02 )    circle = new BlueTarget();

        if ( circle ) {
            circle.x = Math.random() * stage.stageWidth;
            circle.y = Math.random() * stage.stageHeight;
            addChild( circle );
        }

        // Remove dead
        for ( var i:int = 0; i < numChildren; i++ ) {
            var child:Target = getChildAt( i ) as Target;
            if ( child ) {
                if ( child.dead ) {
                    removeChild( child );
                    i--;
                }
            }
        }
    }
}
}

Target.as

package com.multiClicker {
import flash.display.*;
import flash.events.*;

public class Target extends Sprite {
    protected var hp:Number;

    public function 
    Target() {
        addEventListener( MouseEvent.CLICK, onClick );
    }

    public function
    get dead():Boolean {
        if ( alpha <= 0 ) return true;
        return false;
    }

    protected function
    drawTarget( color:uint ):void {
        graphics.beginFill( color );
        graphics.drawCircle( 0, 0, 30 );
        graphics.endFill();
    }

    private function 
    onClick( e:MouseEvent = null ):void {
       if ( --hp <= 0 ) addEventListener( Event.ENTER_FRAME, onEnterFrame );
    }

    private function 
    onEnterFrame( e:Event ):void{
        alpha -= 0.1;
        if ( alpha <= 0 ) {
            removeEventListener( Event.ENTER_FRAME, onEnterFrame );
            removeEventListener( MouseEvent.CLICK, onClick );
        }
    }
}
}

RedTarget.as

package com.multiClicker {
public class RedTarget extends Target {
    public function
    RedTarget() {
        super();
        hp = 3;
        drawTarget( 0xff0000 );
    }
}
}

GreenTarget.as

package com.multiClicker {
public class GreenTarget extends Target {
    public function
    GreenTarget() {
        super();
        hp = 2;
        drawTarget( 0x00ff00 );
    }
}
}

BlueTarget.as

package com.multiClicker {
public class BlueTarget extends Target {
    public function
    BlueTarget() {
        super();
        hp = 1;
        drawTarget( 0x0000ff );
    }
}
}

I hope that helps.



来源:https://stackoverflow.com/questions/14756596/how-to-create-graphics-when-using-classes-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!