Skip to main content
March 25, 2015
Answered

How Can I organize code with frames ?

  • March 25, 2015
  • 1 reply
  • 256 views

Hello everyone,

I try to make a very simple game but I have a question. Would you give me a hand, please ?  I'm not a native English speaker, so I hope you get my question

My game consists of many levels ( a level on each frame ). Each level ( or each frame ) consists of white walls, a green ball, a colorful star, and a yellow key... as you can see in the picture. The green ball should move between the walls without hitting them, and It must hit the key to collect it. When the key is collected, a wall will disappear so that the ball can reach the star, then a player wins. When the ball hits any one of these walls a game over message will appear.

Each frame has different number of walls and each wall is a movie clip on the main stage. The walls are not inside a container or something like that. The instance names of the walls are wall1 , wall2 , wall3 ... and so on.

I also have a layer for action script and I wrote the following code:

if (player.hitTestObject(wall1) || player.hitTestObject(wall2) || player.hitTestObject(wall3) || player.hitTestObject(wall4) )

  {

  trace ( "second try" ) ;

  gameOver.visible = true ;

  doThisFn = false ;

  }


It works for the frames that Has wall1 , wall2 , wall3 and wall4.

If wall4 (for example) doesn't exist on frame 3 (for example),  an error occurs saing : " #2007: Parameter hitTestObject must be non-null. "

My question is, How to organize the action script layer and the frames so that the error doesn't occur ?


I thought of an Idea which is to make a copy of wall4 on layer 3, and I will put wall4 in a place where the user will not see it. I don't think this is a good solution.

I had another Idea, I will make many keyframes ( by pressing F6 ) on the action script layer. and I will modify the code of each keyframe so that the walls that are in IF statement will mach the walls on each frame. but this will take a lot of effort !!


Would you suggest a smarter solution, please ? such as a For loop or an array.. I don't know how ?
Thanks in advance,

.

This topic has been closed for replies.
Correct answer kglad

//you can initialize an array, eg,

var wallA:Array=[];

//reset it on each frame, eg,

wallA.length=0;

//populate it on each frame eg,

for(var i:int=0;i<1000;i++){

if(this['wall'+i]){

wallA.push(this['wall'+i]);

}

// and then use that array in your hittests, eg

function wallHitTestF():Boolean{

for(i=0;i<wallA.length;i++){

if(player.hitTestObject(wallA)){

return true;

}

}

return false;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 26, 2015

//you can initialize an array, eg,

var wallA:Array=[];

//reset it on each frame, eg,

wallA.length=0;

//populate it on each frame eg,

for(var i:int=0;i<1000;i++){

if(this['wall'+i]){

wallA.push(this['wall'+i]);

}

// and then use that array in your hittests, eg

function wallHitTestF():Boolean{

for(i=0;i<wallA.length;i++){

if(player.hitTestObject(wallA)){

return true;

}

}

return false;

}

March 26, 2015

Kglad, Thank you very much for your answer. I understood it but there something that I can not understand .. Would you tell me what this code does ? this['wall'+i] Thank you once again for your help :)

kglad
Community Expert
Community Expert
March 26, 2015

it resolves the string 'wall'+i into an object using the array operator [ ].

p.s when using the adobe forums, please mark helpful/correct responses, if there are any.