使用类时如何创建图形?AS3 [英] How to create graphics when using classes? AS3

查看:20
本文介绍了使用类时如何创建图形?AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个游戏",其中三个不同颜色的圆圈出现在屏幕上,点击不同数量的鼠标使其消失.我有一个主要的 mxml 函数,然后是 4 个其他类.我以为我快要完成它了,但我遇到了一个错误,它没有告诉我它是什么.这是评分细则的链接此处运行代码是:错误#2032:流错误.URL:file:///C|/Users/Gabe/Adobe Flash Builder 4.6/Project 1/bin-debug/framework_4.6.0.23201.swf"这是我的代码:

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

            }
        }

我的 Target.as 文件:

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

    }

}

}

然后目标文件 Red、Blue 和 GreenTarget.as 都是相同的:

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

    }

}

}

我仅在 Target 和 GreenTarget 中遇到错误.它没有告诉我错误是什么或在哪里.任何帮助都会很棒!

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!

我发布了我收到的错误消息.我在 Flash Builder 上使用了 clean 功能,它清除了我的错误.然后我再次运行它,我得到一个找不到文件的错误.它说找不到我的 Main.swf.

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:我想我发现了错误,我删除了 Main.mxml 文件中的 onEnterFrame 函数,错误消失了.现在我只是不明白那个函数有什么问题.

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.

推荐答案

我使用 MiniBuilder 做了类似的事情:

I made something similar using MiniBuilder:

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

以下是课程:

应用程序.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--;
                }
            }
        }
    }
}
}

目标.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

RedTarget.as

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

GreenTarget.as

GreenTarget.as

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

BlueTarget.as

BlueTarget.as

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

希望能帮到你.

这篇关于使用类时如何创建图形?AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆