PDA

View Full Version : [AS2] trying to target game_item3 dynamically


crosshair
04-30-2009, 06:07 PM
As the title suggests, i have a little problem that i cant get my head around.

I have this code:
function gameMover() {
this._x += gameSpeed;

if(home.holder.game_item0._x >= (587-this._width)) {
canMoveR = false;
} else {
canMoveR = true;
}

if(home.holder."game_item"+numOfGames._x <= 30) {
canMoveL = false;
} else {
canMoveL = true;
}
}

I am trying to target game_item3 dynamically as i intend to add many more games and all of the data is imported from an XML doxument.
I have have things like
home.holder["game_item"+numOfGames]
but still no hope...

Thanks in advance!

ImOnCloudNine69
04-30-2009, 06:34 PM
_root["game_item"+numOfGames] is how i would do it, idk if thats different than home.holder


function gameMover() {
this._x += gameSpeed;

if(home.holder["game_item"+0]._x >= (587-this._width)) {
canMoveR = false;
} else {
canMoveR = true;
}

if(home.holder["game_item"+numOfGames]._x <= 30) {
canMoveL = false;
} else {
canMoveL = true;
}
}

crosshair
04-30-2009, 06:35 PM
I set home to _root, same thing but i went to the right directory ;)
And no it doesnt work :(

xxneon
04-30-2009, 06:37 PM
did you trace the value of numOfGames?? is this variable used inside a for loop().. where the value would be changing ?

home.holder["game_item"+numOfGames] would be the proper syntax though .. its just a matter of whether your targeting what you desire .. based on the value of numOfGames..

trace it out .. and see..

crosshair
04-30-2009, 06:45 PM
This is my as...
var gameSpeed; //Speed that games scroll across
var speedMul:Number = 40;
var home:MovieClip = this;
var canMoveR:Boolean;
var canMoveL:Boolean;

var xml:XML = new XML(); //New XML doc
xml.ignoreWhite = true; //Dont know what this does but we need it...

xml.onLoad = function() { //When the XML loads...
//This is where we deal with the games
var gameNodes = this.firstChild.nextSibling.nextSibling.childNodes ; //Targets the games section in the XML
numOfGames = gameNodes.length; //Gets the length of the games section in the XML
for(var i=0;i<numOfGames;i++) {
var g = home.holder.attachMovie("game_item","game_item"+i, 1+i); //Attach all of the games
g._y = 100; //Place y coord
g._x = 180*i; //Space out all of the games so they are not on top of eachother
g.gamename.text = gameNodes[i].attributes.gamename; //Get the game name from the xml
g.gameW = gameNodes[i].attributes.gameW; //Get the game width from the xml
g.gameH = gameNodes[i].attributes.gameH; //Get the game height from the xml
g.gameurl = gameNodes[i].attributes.gameurl; //Get the game url from the xml
g.icon.inner.loadMovie(gameNodes[i].attributes.gameimage); //Get the gamr image from the XML
g.r.inner.loadMovie(gameNodes[i].attributes.gameimage); //Get the game reflection image from the XML
g.onEnterFrame = gameMover; //The game moving function
g.icon.onRollOver = gameOver; //When we hover over the game
g.icon.onRollOut = gameOut; //When we rollout of the game
g.icon.onRelease = gameRelease; //When we click on the gamr
}
}

btnl.onPress = function() {
if(canMoveL) {
gameSpeed = -5;
}
}
btnl.onRelease = function() {
gameSpeed = 0;
}
btnr.onPress = function() {
if(canMoveR) {
gameSpeed = 5;
}
}
btnr.onRelease = function() {
gameSpeed = 0;
}

function gameMover() {
this._x += gameSpeed;

if(home.holder.game_item0._x >= (587-this._width)) {
canMoveR = false;
} else {
canMoveR = true;
}

if(home.holder.game_item3._x <= 30) {
canMoveL = false;
} else {
canMoveL = true;
}
}

function gameOver() {
var sou:Sound = new Sound(); //New sound var
sou.attachSound("sover"); //Attach the sover sound
sou.start(); //Play the sound
}

function gameRelease() {
var sou:Sound = new Sound(); //New sound var
sou.attachSound("sdown"); //Attach sdown sound
sou.start(); //Play the sound
openWinCentre(gameurl, gamename, gameW, gameH, 0, 0, 0, 0, 0, 0, 0);
}

xml.load("website.xml"); //Load the XML

xxneon
04-30-2009, 07:13 PM
oh ok .. this will work..
home.holder["game_item"+(numOfGames-1)]
if you use numOfGames.. its value is 4.. since 0 - 3 equals 4 items.. there is no game_item4.. so you have to subtract 1 to get the last item..

crosshair
04-30-2009, 07:16 PM
That works fine, thanks :)