简体   繁体   中英

as3 error loading “good” “bad” array

I am creating a "good" "bad" array to display on screen which i can later use simple if statements to do something upon if the player has collided with the "good" "bad" objects. I cant get the objects to randomly generate on screen with the following code.

// Create and Set good/bad random Word objects

public function newObject(e:Event) 
    {
        var goodObjects:Array = ["WordObject1"];
        var badObjects:Array = ["WordObject2"];
        if (Math.random() < .5) 
        {
            var r:int = Math.floor(Math.random()*goodObjects.length);
            var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
            var newObject:MovieClip = new classRef(); 
            newObject.typestr = "good";
        } else 
        {
            r = Math.floor(Math.random()*badObjects.length);
            classRef = getDefinitionByName(badObjects[r]) as Class;
            newObject = new classRef(); 
            newObject.typestr = "bad";
        }
        newObject.x = Math.random();
        newObject.y = Math.random();
        addChild(newObject);
        objects.push(newObject);
        placeWords();
    }
    // create random Word objects 
    public function placeWords() {
        objects = new Array();
        for(var i:int=0;i<numWordObjects;i++) {

            // loop forever
            while (true) {

                // random location
                var x:Number = Math.floor(Math.random()*mapRect.width)+mapRect.x;
                var y:Number = Math.floor(Math.random()*mapRect.height)+mapRect.y;

                // check all blocks to see if it is over any
                var isOnBlock:Boolean = false;
                for(var j:int=0;j<blocks.length;j++) {
                    if (blocks[j].hitTestPoint(x+gamesprite.x,y+gamesprite.y)) {
                        isOnBlock = true;
                        break;
                    }
                }

                // not over any, so use location
                if (!isOnBlock) {
                    newObject.x = x;
                    newObject.y = y;
                    newObject.gotoAndStop(Math.floor(Math.random()*1)+1);
                    gamesprite.addChild(newObject);
                    objects.splice(newObject);
                    break;
                }
            }
        }
    }

i get the following errors:

1119: Access of possibly undefined property x through a reference with static type Function.
1119: Access of possibly undefined property y through a reference with static type Function. 1067: Implicit coercion of a value of type Function to an unrelated type flash.display:DisplayObject.

Try renaming var x and var y to something else. Those are public properties in any class that extends as a display object (Sprite/MovieClip/Shape).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM