0

我正在创建一个 Flash 游戏,其中物体从天而降,玩家需要通过点击它们来摧毁它们。但是我有一个问题,有时它们会在彼此之上生成一个。这是我的意思的一个例子:

错误的产卵

对象应该在另一个附近产生,但不是一个在另一个上。

这是我的常量变量:

public static const GRAVITY:Number = 3;
public static const HIT_TOLERANCE:Number = 50;  

//Powerup
public static const APPLE_END_Y:Number = 640;
public static const APPLE_SPAWN_CHANCE:Number = 0.02; //per frame per second
public static const APPLE_START_Y:Number = 110;
public static const APPLE_SPAWN_START_X:Number = 50;
public static const APPLE_SPAWN_END_X:Number = 500;

//Scoring
public static const PLAYER_START_SCORE:Number = 0;
public static const SCORE_PER_APPLE:Number = 10;

这是生成对象的代码的一部分:

private function update(evt:Event)
{

    //Spawn new apples
    if (Math.random() < randomChance)
    {
        //spawn x  coordinates
        var newPirmas = new Pirmas();
        newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newAntras = new Antras();
        newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newTrecias = new Trecias();
        newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newApple = new Apple();
        newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        //spawn y coordinates
        newPirmas.y = C.APPLE_START_Y;
        newAntras.y = C.APPLE_START_Y;
        newTrecias.y = C.APPLE_START_Y;
        newApple.y = C.APPLE_START_Y;

        newPirmas.addEventListener(MouseEvent.CLICK, onClick);
        newAntras.addEventListener(MouseEvent.CLICK, onClick);
        newTrecias.addEventListener(MouseEvent.CLICK, onClick);
        newApple.addEventListener(MouseEvent.CLICK, onClick);

        itemsToSpawn.push(newPirmas, newAntras, newTrecias, newApple);
    }   
}        

正如有人所说:As for making sure they don't overlap, you can keep a history of their spawn points, and change how you get their random X value. Just iterate through the array of previous X values, and make sure the new one isn't within (oldX + (width/2)).

但我无法成功,你能帮我保存他们的出生点历史吗?非常感谢。

4

1 回答 1

0
var t:Array = [];
t[0] = [APPLE_SPAWN_START_X, APPLE_SPAWN_END_X + APPLE_WIDTH/2];

t 将保留您可以生成新苹果的可用间隔。开始时间没有apple,t只有一个区间,区间的starX为APPLE_SPAWN_START_X,区间的endX为APPLE_SPAWN_END_X + APP_WIDTH/2。

在你想生成一个新苹果的地方,你会在 t 中找到一个可用的间隔,如果我们找到间隔为 mInterval(这个间隔可以放在苹果中,意味着 endX - starX >= APPLE_WIDTH),那么我们可以生成你的新苹果.

我们只需要更改您更新功能中的行,例如

newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;

反而

newPirmas.x = mInterval[0] + Math.random()*APPLE_WIDTH;

当我们设置 newPirmas.x 时,我们打破了 mInterval 并生成了两个新的区间,其值为 [mInterval[0],newPirmas.x] 和 []newPirmas.x + APPLE_WIDTH, mInterval[0]],所以我们需要删除 t 中的 mInterval 并将两个新的区间放入 t 中。

    /**
     * 
     * @return index of available interval in target array
     */
    public static function findAvailInterval(target:Array, appleWidth:int):int {

        if (target == null || target.length == 0) {
            return -1;
        }

        var endX:int = 0;
        var startX:int = 0;

        var length:int = target.length;

        for (var i:int = 0; i < length; i++) {

            var temp:Array = target[i] as Array;

            if (temp == null || temp.length < 2) {
                return -1;
            }

            startX = temp[0];
            endX = temp[1];

            var intervalWidth:int = endX - startX;//the interval width

            if (intervalWidth >= appleWidth) {//find an available one
                return i;
            }
        }

        return -1;
    }

    public static function breakInterval(target:Array, appleWidth:int, index:int):int {
        var temp:Array = target[index];

        var startX:int = temp[0];
        var endX:int = temp[1];

        var width:int = endX - startX;

        var availableNewXRange:int = width - appleWidth;//the apple‘s max x, and the min x is startX

        //random a position
        var appleX:int = startX + availableNewXRange*Math.random();

        var leftInterval:Array = [startX, appleX];//the left interval of apple

        var rightInterval:Array = [appleX+appleWidth, endX];//the right interval of apple

        //delete temp
        target.splice(index, 1);

        if (isAvailableInterval(leftInterval, appleWidth)) {
            target.push(leftInterval);
        }

        if (isAvailableInterval(rightInterval, appleWidth)) {
            target.push(rightInterval);
        } else {
            trace("vvv");
        }

        return appleX;
    }

    private static function isAvailableInterval(interval:Array, appleWidth:int):Boolean {

        if (interval == null || interval.length < 2) {
            return false;
        }

        return (interval[1] - interval[0]) >= appleWidth;
    }

把这三个函数放到一个类A中

        var target:Array = [[0, 1000]];
        var appWidth:int = 80;
        for (var i:int = 0; i < 4; i++) {
            var index:int = A.findAvailInterval(target, appWidth);

            if (index != -1) {
                var interval:Array = target[index];

                RR.breakInterval(target, appWidth, index);

                trace(interval);
            }
        }
于 2013-06-04T08:05:37.123 回答