AS3 数组 indexOF
AS3 array indexOF
我正在开发游戏,但遇到了这个数组的问题。舞台上的所有果实都是阵果生成的。当上诉将命中篮筐时,我们将计算它们。瓜不可数。我如何知道当前的水果是苹果还是甜瓜?
var array_fruit:Array = new Array(appel1_mc, appel2_mc, melon_mc);
var appelsOnstage:Array = new Array();
var appelsCollected:Number = 0;
for (var i:int = 0; i<10; i++) {
var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
var spel_appels:MovieClip = new pickappels();
addChild(spel_appels);
spel_appels.x = (Math.random() * 800) + 100;
spel_appels.y = Math.random() * -500;
spel_appels.speed = Math.random() * 10 + 2;
appelsOnstage.push(spel_appels);
}
stage.addEventListener(Event.ENTER_FRAME, catchappels);
function catchappels(e:Event):void {
for (var i:int = appelsOnstage.length-1; i > -1; i--) {
var currentfruit:MovieClip = appelsOnstage[i];
if (currentfruit.hitTestObject(basket_mc)) {
if(array_fruit.indexOf(currentfruit) == melon_mc ){
trace("melon");
} else {
trace("no melon");
appelsCollected++;
}
}
}
}
indexOf
将要 return 一个 int
值。在下面查看我对您的代码的编辑。
if (currentfruit.hitTestObject(basket_mc)) {
if(array_fruit.indexOf(currentfruit) == 2 ){
trace("melon");
} else {
trace("no melon");
appelsCollected++;
}
}
indexOf
有两个参数。第一个是您正在搜索的确切元素。第二个参数是开始搜索的索引位置。默认为位置 0.
请参阅 API 文档 here。 (最好先快速阅读一下)。
a_fruit.indexOf(currentfruit) = -1 all the time
这里的问题是 classes 和对象之间的混淆。
a_fruit
包含 classes,将由此代码实例化
var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
var spel_appels:MovieClip = new pickappels();
appelsOnstage
包含 个对象 class 并在此处填充
appelsOnstage.push(spel_appels);
类 和对象是 - 请原谅双关语! - 苹果和橙子,非常不同的东西。
class 就像建筑的蓝图或一顿饭的食谱。您不能将对象与 classes 进行比较并期望它们相同。
相反,您应该找到一个对象的 class,然后将此 class 与另一个 class 进行比较。为此,您使用 is
operator。像
if(currentfruit is melon_mc)
应该会告诉你有没有瓜。
你应该在这里使用 is
运算符,以便更好地编码。如果您要更改数组中 类 的位置怎么办?
if ((currentfruit is appel1_mc) || (currentfruit is appel2_mc)) {
// apple
}
只是用伪代码来找出处理这个问题的可能方法。
在时间轴上:
import com.fruits.Fruit;
import com.fruits.Melon;
import com.fruits.Apple
var fruit_1:Melon = new Melon();
var fruit_2:Apple = new Apple();
if(fruit_1 is Melon){
trace("anyway my SuperclassName is : " + fruit_1.getType());
trace (fruit_1.getMyType());
trace("");
}
if(fruit_2 is Apple){
trace("anyway my SuperclassName is : " + fruit_2.getType());
trace (fruit_2.getMyType());
trace("");
}
在 com.Fruit.as 中:
package com.fruits {
public class Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Fruit() {
trace ("I'm a Fruit");
}
public function getType():String{
var type:String = getQualifiedSuperclassName(this)
var str:String = (type);
return str;
}
}
}
在 com.Melon 中:
package com.fruits {
public class Melon extends Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Melon() {
super();
trace ("Melon says : ");
trace (" because I'm Fruit and not happy to be a Melon");
trace("");
}
public function getMyType():String{
var type:String = getQualifiedClassName(this)
var str:String = ("Im a " + type);
trace("Class said : I worth nothing because I'm an Fruit and not proud to be an Melon");
str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
return str;
}
}
}
在com.Apple中:
package com.fruits {
public class Melon extends Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Melon() {
super();
trace ("Melon says : ");
trace (" because I'm Fruit and not happy to be a Melon");
trace("");
}
public function getMyType():String{
var type:String = getQualifiedClassName(this)
var str:String = ("Im a " + type);
trace("Class said : I worth nothing because I'm an Fruit and not proud to be an Melon");
str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
return str;
}
}
}
这只是一个想法,请告诉我更多关于您的目标的信息,如果这可能对您有所帮助...
最好的问候尼古拉斯。
PS : 对不起,我的英文真的很烂。所以我试着找出问题所在...
你可能会忘记 indexOf 并随时使用 addChild 你想在另一个上设置一个 object/instance....
所以你不必检查索引。
addChild(some old Child) 将使 "old Child" 位于最后一个索引(在其他实例之上)。
对不起,我没有写你的代码,但标题说你想检查水果是甜瓜还是苹果......
对不起,如果我误解了。 :(
我正在开发游戏,但遇到了这个数组的问题。舞台上的所有果实都是阵果生成的。当上诉将命中篮筐时,我们将计算它们。瓜不可数。我如何知道当前的水果是苹果还是甜瓜?
var array_fruit:Array = new Array(appel1_mc, appel2_mc, melon_mc);
var appelsOnstage:Array = new Array();
var appelsCollected:Number = 0;
for (var i:int = 0; i<10; i++) {
var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
var spel_appels:MovieClip = new pickappels();
addChild(spel_appels);
spel_appels.x = (Math.random() * 800) + 100;
spel_appels.y = Math.random() * -500;
spel_appels.speed = Math.random() * 10 + 2;
appelsOnstage.push(spel_appels);
}
stage.addEventListener(Event.ENTER_FRAME, catchappels);
function catchappels(e:Event):void {
for (var i:int = appelsOnstage.length-1; i > -1; i--) {
var currentfruit:MovieClip = appelsOnstage[i];
if (currentfruit.hitTestObject(basket_mc)) {
if(array_fruit.indexOf(currentfruit) == melon_mc ){
trace("melon");
} else {
trace("no melon");
appelsCollected++;
}
}
}
}
indexOf
将要 return 一个 int
值。在下面查看我对您的代码的编辑。
if (currentfruit.hitTestObject(basket_mc)) {
if(array_fruit.indexOf(currentfruit) == 2 ){
trace("melon");
} else {
trace("no melon");
appelsCollected++;
}
}
indexOf
有两个参数。第一个是您正在搜索的确切元素。第二个参数是开始搜索的索引位置。默认为位置 0.
请参阅 API 文档 here。 (最好先快速阅读一下)。
a_fruit.indexOf(currentfruit) = -1 all the time
这里的问题是 classes 和对象之间的混淆。
a_fruit
包含 classes,将由此代码实例化var pickappels = array_fruit[int(Math.random()* array_fruit.length)]; var spel_appels:MovieClip = new pickappels();
appelsOnstage
包含 个对象 class 并在此处填充appelsOnstage.push(spel_appels);
类 和对象是 - 请原谅双关语! - 苹果和橙子,非常不同的东西。 class 就像建筑的蓝图或一顿饭的食谱。您不能将对象与 classes 进行比较并期望它们相同。
相反,您应该找到一个对象的 class,然后将此 class 与另一个 class 进行比较。为此,您使用 is
operator。像
if(currentfruit is melon_mc)
应该会告诉你有没有瓜。
你应该在这里使用 is
运算符,以便更好地编码。如果您要更改数组中 类 的位置怎么办?
if ((currentfruit is appel1_mc) || (currentfruit is appel2_mc)) {
// apple
}
只是用伪代码来找出处理这个问题的可能方法。 在时间轴上:
import com.fruits.Fruit;
import com.fruits.Melon;
import com.fruits.Apple
var fruit_1:Melon = new Melon();
var fruit_2:Apple = new Apple();
if(fruit_1 is Melon){
trace("anyway my SuperclassName is : " + fruit_1.getType());
trace (fruit_1.getMyType());
trace("");
}
if(fruit_2 is Apple){
trace("anyway my SuperclassName is : " + fruit_2.getType());
trace (fruit_2.getMyType());
trace("");
}
在 com.Fruit.as 中:
package com.fruits {
public class Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Fruit() {
trace ("I'm a Fruit");
}
public function getType():String{
var type:String = getQualifiedSuperclassName(this)
var str:String = (type);
return str;
}
}
}
在 com.Melon 中:
package com.fruits {
public class Melon extends Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Melon() {
super();
trace ("Melon says : ");
trace (" because I'm Fruit and not happy to be a Melon");
trace("");
}
public function getMyType():String{
var type:String = getQualifiedClassName(this)
var str:String = ("Im a " + type);
trace("Class said : I worth nothing because I'm an Fruit and not proud to be an Melon");
str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
return str;
}
}
}
在com.Apple中:
package com.fruits {
public class Melon extends Fruit {
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public function Melon() {
super();
trace ("Melon says : ");
trace (" because I'm Fruit and not happy to be a Melon");
trace("");
}
public function getMyType():String{
var type:String = getQualifiedClassName(this)
var str:String = ("Im a " + type);
trace("Class said : I worth nothing because I'm an Fruit and not proud to be an Melon");
str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
return str;
}
}
}
这只是一个想法,请告诉我更多关于您的目标的信息,如果这可能对您有所帮助... 最好的问候尼古拉斯。
PS : 对不起,我的英文真的很烂。所以我试着找出问题所在...
你可能会忘记 indexOf 并随时使用 addChild 你想在另一个上设置一个 object/instance.... 所以你不必检查索引。 addChild(some old Child) 将使 "old Child" 位于最后一个索引(在其他实例之上)。 对不起,我没有写你的代码,但标题说你想检查水果是甜瓜还是苹果...... 对不起,如果我误解了。 :(