For loop does not start with Array

Asked

Viewed 200 times

2

I’m new to AS3, and I’m trying to create a game for a course I’m doing, but I’m having trouble starting a loop that should test the collision between two objects called by AS3.

Because objects are called dynamically by AS3, I’m inserting them into array to refer them, and it works well when I identify them by the fixed number in array, but I want him to be able to test with all the objects in array, and the result is that the loop doesn’t even start.

Since I didn’t want him to do the tests needlessly, I put the bow on after one if to check if there were objects in the array, and I thought the if that didn’t work, so I inserted some commands trace, and then I knew he was in if, but just didn’t start the loop for.

For loop that is not working: Laço FOR que não funciona

Output: Resultado na saída

2 answers

4


I think the problem lies in the statement of the:

for(alvo = 0; alvo >= lastEnemy; alvo++) 

I think it should be:

for(alvo = 0; alvo <= lastEnemy; alvo++)  

or:

for(alvo = 0; alvo < lastEnemy; alvo++)  

alvo >= lastEnemy is fake right at the loop input

2

You seem to be not standing firm as to how the loop for works. Recapitulating:

for ( iniciação ; condição de permanência ; passo ao final ) { corpo }
  • initialization: code to be executed before to start the loop. It at all times executes, regardless of whether the condition is met or not.
  • condition of permanence: test to be performed before to enter the body. He at all times forehead, even before entering the body for the first time. If the result is true, it enters the body if it is false he does not enter and proceed to the next instruction (i.e. that after the for).
  • body: code that runs multiple times, until the condition of permanence becomes false or to come out abruptly through a break, return or in case of an exception.
  • step to the end: code to be executed afterward body. If the body ends normally, or if the instruction continue is used, this step will be executed soon after. It occurs before of the condition of permanence to be tested again.

Looking at your code, it seems to me that your intention is to go through the entire list of objects, right? Thus, your target should follow from the first index in the list to the last. As you already know, Actionscript, indexing starts from scratch, so the first index (initiation) is correct. The step is also correct (increase one in the index) and the final index (lastEnemy) also - because it is the size of the list minus one. The problem is in the condition of permanence, so.

As summarized in reply from @ramaral, your condition of permanence (alvo >= lastEnemy) is false even before starting the loop (since the list of objects is greater than zero, confome tested by your if - otherwise you would have an infinite loop). This leads me to believe that you were trying to create a stop condition instead, am I right? Anyway, this stopping condition would not be correct, since it would leave as soon as alvo was the same as lastEnemy (and you want him to leave soon after that). That is, stop if alvo > lastEnemy.

The solution then is to deny the stopping condition [corrected], to obtain the condition of permanence. !(alvo > lastEnemy) which is equivalent to alvo <= lastEnemy. That way, he goes continue in the loop while alvo is less than or equal to lastEnemy, and leave as soon as that condition becomes false.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.