jeudi 30 novembre 2017

AS3 : Unable to acces object method via an ObjectContainer

first of all, i'm not a native english speaker but, still, i'll try my best to be understandable and as clear as possible.

So, in my programming class, I need to make a Tile based game (like zelda, for exemple) with animate cc (flash). On a map, I want to make a dance floor with tiles that changes on the rhythm of a music. these tiles are movieclip with two frame, one white and one red.

This is how the tiles are generated:

private function createGrid(): void {

        grid = new MovieClip();
        addChild(grid);
        for (var r: int = 0; r < nbRow; r++) {
            for (var c: int = 0; c < nbCol; c++) {
                var t: Tiles = new Tiles();
                t.x = t.width * c;
                t.y = t.height * r;
                grid.addChild(t);
            }
        }

        grid.x = 15; //center the grid on x
        grid.y = 35; //center the grid on y
}

This is the Tiles Class :

package {
import flash.display.MovieClip;
import flash.events.*;

public class Tiles extends MovieClip {
    private var rand:int;

    public function Tiles() {
        // constructor code
        getTiles();
    }
    public function getTiles():void {
        random();
        setColor();
    }
    private function random() : void{
        rand = Math.floor(Math.random()*100)+1;
    }

    private function setColor() : void{
        if(rand<=30){
            gotoAndStop(8); //red frame
        }else{
            gotoAndStop(7); //white frame
        }
    }
}

}

createGrid() place the tiles as soon as the map is placed on the stage and stock every tiles in the MovieClip grid. Now, I want the tiles to change randomly between red and white on the beat of a streamed music (and keep the ratio of 30% red tiles and 70% white tiles)

var s: Sound = new Sound();
var sc: SoundChannel;

s.load(new URLRequest("GameSong_mixdown.mp3"));
sc = s.play(0, 1000);

I know i need the leftpeek properties of my soundchannel to achieve that but,for now, I do my test with a button that trigger this function:

private function setTiles(e: Event): void {
        // loop through all child element of a movieclip
        for (var i: int = 0; i < grid.numChildren; i++) {
            grid.getChildAt(i).getTiles();          
        }
    }

Right now, the problem is : I'm unable to acces my Tiles method. I did a trace on grid,getChildAt(i), and saw all instances of my tiles in the console. So, i know for sure that every instances of my tiles are stored in grid. But, I don't know why, grid.getChildAt(i).getTiles(); doesn't work (and every other method from Tiles). The error message is: Call to a possibly udefined method getTiles through a reference with static type flash.display:DisplayObject

Does someone know what i'm doing wrong ?

ps: I translated all my class name, var name, etc from french to english to make the code clearer.




Aucun commentaire:

Enregistrer un commentaire