【问题标题】:How to check if two objects are same color in a hit test in AS3如何在 AS3 的命中测试中检查两个对象是否相同颜色
【发布时间】:2015-10-27 00:29:42
【问题描述】:

我的程序是一个简单的游戏,其中彩色矩形落在屏幕上。您的播放器会根据您单击的按钮更改颜色,目的是使您的播放器与下落物体的颜色相同。我如何做一个命中测试对象并检查两种颜色是否相同。

这是我的代码:

import flash.geom.ColorTransform;
import flash.events.TimerEvent;

var rectangle:Shape = new Shape;
var RecTimer:Timer = new Timer(5);
var RecSTimer:Timer = new Timer(800);
var collision:Timer = new Timer(10,1000);
collision.start()
RecTimer.addEventListener(TimerEvent.TIMER, onTimer);
RecTimer.start();
RecSTimer.addEventListener(TimerEvent.TIMER, onSpawnTimer);
RecSTimer.start();
collision.addEventListener(TimerEvent.TIMER, fcollision)


function fcollision(e:TimerEvent):void {
    for each(var rectangle:Shape in rectangles)
    {
        if (mcPLayer.hitTestObject(rectangle)) {

       }
        }
    }

var rectangles:Array = []; // a list of all the rectangles we've made so far
 function spawnRectangle():void {
    var rectangle:Shape = new Shape();
    rectangle.graphics.beginFill(randomColor()); // choosing the colour for the fill, here it is red
    rectangle.graphics.drawRect(0, 10, 480, 45.49); // (x spacing, y spacing, width, height)
    rectangle.graphics.endFill();
    addChild(rectangle); // adds the rectangle to the stage
    rectangles.push(rectangle); // adds the rectangle to our list of rectangles




}
var colors:Array = [0xFF0000, 0x00FF00, 0x0000FF];

function randomColor():uint
{
    return colors[int(Math.random()*colors.length)];
}
function moveAllRectangles():void {
    for each (var rectangle:* in rectangles) {
            rectangle.y +=2;
        if (rectangle.y == 550){
            removeChild(rectangle)
        }

                    }

    }

function onTimer(e:TimerEvent):void {
        moveAllRectangles();

}
function onSpawnTimer(e:TimerEvent):void {
    spawnRectangle();
}
btnRed.addEventListener(MouseEvent.CLICK, fred);
btnGreen.addEventListener(MouseEvent.CLICK, fgreen);
btnBlue.addEventListener(MouseEvent.CLICK, fblue);

function fred (e:MouseEvent):void{
 var myColorTransform = new ColorTransform();
myColorTransform.color = 0xFF0000;
mcPLayer.transform.colorTransform = myColorTransform;
}

function fgreen (e:MouseEvent):void{
 var myColorTransform = new ColorTransform();
myColorTransform.color = 0x00FF00;
mcPLayer.transform.colorTransform = myColorTransform;
}
function fblue (e:MouseEvent):void{
 var myColorTransform = new ColorTransform();
myColorTransform.color = 0x0066CC;
mcPLayer.transform.colorTransform = myColorTransform;
}

function delayedFunctionCall(delay:int, func:Function) {
    var timer:Timer = new Timer(delay, 1);
    timer.addEventListener(TimerEvent.TIMER, func);
    timer.start();
}

【问题讨论】:

  • obj1.transform.colorTransform.color == obj2.transform.colorTransform.color 呢?
  • 你考虑过扩展Shape 给它一个公共变量来存储它的颜色吗?然后你可以将它与玩家当前颜色的变量进行比较。
  • 如何将Shape 扩展到公共变量。
  • 我的解决方案不适合您吗?扩展 Shape 将涉及创建一个类文件。你以前创建过自定义类吗?

标签: actionscript-3 colors hittest


【解决方案1】:

有几种方法可以实现这一点。一种简单的方法是执行 cmets 中建议的操作并使用 ColorTransform。

看起来像这样:

//call this function anytime the player needs to change color (and also right at the start with the default color)
function changePlayerColor(color:uint):void {
    //create a new color transform object
    var clr:ColorTransform = new ColorTransform();
    clr.color = color; //give it a color
    mcPlayer.transform.colorTransform = clr; //attach it to the player (which just tints the movie clip with that color)
} 

function spawnRectangle():void {
    var rectangle:Shape = new Shape();
    rectangle.graphics.beginFill(0); //doesn't matter what color you fill with, as the color transform below will tint it

    //create a new color transform, then assign the color property
    var clr:ColorTransform = new ColorTransform();
    clr.color = randomColor();

    //assign the newly created color transform to the rectangle
    rectangle.transform.colorTransform = clr;

    rectangle.graphics.drawRect(0, 0, 480, 45.49);
    rectangle.graphics.endFill();
    addChild(rectangle);

    rectangle.x = 0;
    rectangle.y = 10;

    rectangles.push(rectangle);
}

function fcollision(e:TimerEvent):void {
    for each(var rectangle:Shape in rectangles){
        if (mcPLayer.hitTestObject(rectangle) && mcPlayer.transform.colorTransform.color === rectangle.transform.colorTransform.color) {
            //there was a hit test, and the colors match
        }
    }
}

如果上述内容过于简单(例如,出于美学原因,您不希望播放器染成纯色),那么您可以向播放器添加一个动态属性,告诉您其颜色:

function changePlayerColor(color:uint):void {
     //since movie clips are dynamic, you can just make up a property on it and give it a value like the next line
     mcPlayer.myColor = color;

     //now do what need to do to make the player look the way you'd like
}

然后检查:

if (mcPLayer.hitTestObject(rectangle) && mcPlayer.myColor === rectangle.transform.colorTransform.color){

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多