洗牌数组,数组拼接。动作脚本2 [英] Shuffle Array, Splice Array. Action script 2

查看:154
本文介绍了洗牌数组,数组拼接。动作脚本2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好程序员。我试图创建动作脚本游戏2的概念是,游戏随机选择的对象。球员有获得对象,那么游戏召唤出不同的随机对象。 (这些对象是在阵列)。

Good afternoon programmers. I am trying to create a game in action script 2. the concept is that the game randomly picks an object. The player has to get the object, then the game calls out a different random object. (these objects are in an array).

static private var oneArray:Array = new Array("milk","cheese","bread");
static private var randomItem:String = oneArray[Math.floor(Math.random() * oneArray.length)];

所以,你可以看到我已经做到了这一点。
现在的游戏会显示文本;

So you can see I've done this. Now the game will display the text;

    _root.hud.getItem.text = randomItem;

从code上面可以看到文本字段将从阵列选择一个randomeItem。

From the code above you can see that the text field will pick a randomeItem from that array.

    if (randomItem == ("milk"))
    {
        if (_root.milk.hitTest(_root.player._x + 60, _root.player._y - 60, true) || _root.milk.hitTest(_root.player._x - 60, _root.player._y - 60, true)) 
        {
            trace("got milk");
            _root.milk.gotoAndStop(2);
        }
    }

在code您在上面看到意味着如果文本字段显示奶,如果玩家接触牛奶,那么牛奶中消失了。

The code you see above means that if the text field displays "milk" and if the player touches the milk, then the milk goes away.

现在当碰撞是真实的,我想文本框来显示从数组另一个随机项目,但我不希望同样的名字出现。
我试过拼接牛奶从数组,但仍文本框显示文本奶。

Now when the collision is true, I want the textfield to display another random item from the array, but I don't want the same name to appear. I've tried splicing milk from the array but the textfield still displays the text "milk".

这基本不更新。

我已经尽我所能,和对不起,如果上面提供的信息是不是最好的。
是否有人可以帮助我。
谢谢你。

I've tried my best, and sorry if the information provided above isn't the best. Can someone please help me. Thank you.

推荐答案

交换最后也是随机选择的项目在 oneArray ,并减少随机乘数( pviously随机选取LEN ),所以最后一个项目($ p $)将永远不会再打扰。

Swap last and random selected item in oneArray, and decrease random multiplier(len) so the last item(previously random selected) will never bother again.

private var oneArray:Array = new Array("milk","cheese","bread");
private var len:int = oneArray.length - 1;
private var rand:int;
private var randomItem:String;

选择随机项目

rand = Math.floor(Math.random() * len);
randomItem = oneArray[rand];
_root.hud.getItem.text = randomItem;

碰撞是真实的。

var item:String = oneArray[rand];
oneArray[rand] = oneArray[len];
oneArray[len] = item;
len--;

if (len < 0) { 
    //goto next level
}
else {
   // and select random item again
}

这篇关于洗牌数组,数组拼接。动作脚本2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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