2

I am working on a game and have trouble with this array. There is all fruits on the stage which is generated from the array fruit. When the appel will hit the basket, we will count them. The melon is not to be counted. How do i find out if the current fruit is an appel or a melon?

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++;
            }
    }
 }
}
4
  • What is appelsOnstage? Commented Oct 30, 2016 at 13:57
  • I put some more code here. appelsOnstage is an array containing the appels on the stage. Commented Oct 30, 2016 at 15:06
  • array_fruit.indexOf(currentfruit) = -1 all the time Commented Oct 30, 2016 at 17:47
  • Your updates are appreciated and clarified the question. Commented Oct 30, 2016 at 18:06

4 Answers 4

1

a_fruit.indexOf(currentfruit) = -1 all the time

The problem here is the confusion between classes and objects.

  • a_fruit holds classes, which are to be instantiated by this code

    var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
    var spel_appels:MovieClip = new pickappels();
    
  • appelsOnstage holds objects of those classes and is filled here

    appelsOnstage.push(spel_appels);
    

Classes and objects are - excuse the pun! - apples and oranges, very different things. A class is like a blueprint for a building or a recipe for a meal. You cannot compare objects with classes and expect them to be the same.

Instead, you should find the class of an object, and then compare this class to another class. For this, you use the is operator. Something like

if(currentfruit is melon_mc)

should do the trick to tell you if you have a melon or not.

Sign up to request clarification or add additional context in comments.

4 Comments

the output showed me indeed [object melon_mc] and [class melon_mc] so I tried the suggestion of curretfruit == melon_mc as well and that gives an error about static.flash.movieclip can not compare with classes.
@Henrique I'm not quite sure what you are trying to say. I never suggested to use currentfruit == melon_mc. Is your problem solved with what I suggested in my answer or not? If not what exactly have you tried and what was the exact error you received?
Ok, thank you for your help. I will make a different array for the other fruit.
I think the problem was indeed the confusion between objects and classes and I made two arrays now, one for the apple's and one for the fruit and that works perfectly.
0

indexOf is going to return an int value. See my edit to your code below.

if (currentfruit.hitTestObject(basket_mc)) {        
    if(array_fruit.indexOf(currentfruit) == 2 ){
       trace("melon");
    } else {
       trace("no melon");
       appelsCollected++;
    }
}

indexOf takes two arguments. The first is the exact element you are searching for. The second argument is the index position to start the search from. The default is position 0.

See the API documentation here. (Always good to give these a quick read first).

3 Comments

Sorry for my long answer @NealDavis I try my best to understand English :( I'm sure that null and You have a better answer. :(
Do you're able to understand me when I write in English? Best regards! Nicolas.
You do quite well with English! No need to apologize. We should keep these comments to the matter at hand, though, I think.
0

You should use is operator here, for better coding. What if you'd change the positions of classes in your arrays?

if ((currentfruit is appel1_mc) || (currentfruit is appel2_mc)) {
    // apple
}

Comments

0

Just in pseudo code to figure a possible way to deal with this issue. On the timeline :

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

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

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

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

This is just an idea, tell me more about your target and if this may help You... Best regards Nicolas.

PS : I'm sorry but my English is really bad. so I tried to figure me the issue...

You may perhaps forget about indexOf and use addChild anytime You want to set an object/instance over another one.... So You don't have to check about the indexes. addChild(some old Child) will make the "old Child" be on the the last index (over the other instances). Sorry I didn't wrote your code but the title said that you want to check if the fruit is a Melon or an Apple... Sorry if I misunderstood. :(

4 Comments

I'm sure that @null have understand your question, so read him first! I just do my best This is a good exercise for me
indexOf has nothing to do with z-order (which is what you're referring to with your addChild comment). indexOf refers to the index position in an array. As in var arr:Array = [6,5,4,3,2,1]; trace(arr.indexOf(6)); returns 0 because 6 is in the 0th position of the array.
Sorry @NealDavis, I try to to my best to understand and answer. Most of time, I don't really understand the question. I just do my best to learn English and don't forget AS3 because I had to stop my work due to health reasons. THX for your comments! I really appreciate them!
Now, I finally understood your comment. My fault!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.