0

I'm trying to make a loop that won't crash my flash application. I want variable CN to go from 1 to 10, and then 10 turns into 1 (1,2,3,4,5,6,7,8,9,10,1....). This is what I have so far...

var CN:int = 1;
for(int CN = 1; CN<100; CN++);

NumberCounter.text = String(CN);

Please help. I don't get this at all :( I'm a novice programmer so a lot of things I do won't make much sense.

7
  • What do you mean by 'turns into 1'? Do you mean starts over or just puts 1 at the end? Commented Nov 22, 2013 at 22:58
  • Do you have a maximum number of times you need it to start over? If not, the app will just get caught in an infinite loop. Perhaps if you give a few details about the purpose of this, we can recommend a more appropriate approach to the problem. Commented Nov 22, 2013 at 23:08
  • BTW, in the code snippet above, I don't think you need the 'var CN:int=1;' You are creating it again in your FOR statement. Commented Nov 22, 2013 at 23:10
  • I'm in awe of this question/answer. The goal being to achieve an infinite loop. Surely taking the time to learn about the basics of a for loop is more efficient and beneficial for learning than what we have here. - dev.tutsplus.com/tutorials/as3-101-loops--active-1878 -- via google search for "as3 tutorial for loop" Commented Nov 23, 2013 at 0:29
  • @prototypical - I think you should consider replacing your cynicism with understanding. For a 'novice' programmer, it can be difficult to know how to approach these types of problems. I know from my own experience, when I first started programming, it took some time to learn about resources like Google and 'tutorial'. Commented Nov 23, 2013 at 1:14

4 Answers 4

1

Your question is a bit unclear. Are you trying to go from 1 to 10 then from 10 to 1 once (20 steps) or to go back and forth between 1 and 10 in 100 steps ?

If it's the first, you can try something like this:

for(var i:int = 0, j:int = 0; i < 20; i++){
   if(i < 10) j++;
   else       j--;
   trace(j);//put this in your text field
}

if it's the second:

for(var i:int = 0; j:int = 1, k:int = 0; i < 100; i++){
   if(i % 10 == 0) j *= -1; //every 10 steps flip (multiply by -1) the increment direction(increase/decrease)
   k += j;//increment k based on j which will either increase or decrease
   trace(k);//use this value
}

However the textfield will update right away. If you want to display this change in time you can use the ENTER_FRAME event to increment (rather than the for loop) or a tween engine to animate the value

Sign up to request clarification or add additional context in comments.

1 Comment

Its ok I decided to do something else instead of a variable loop. But this is great advice for next time :D
0

For an infinite loop:

while(true){

  for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}

To do it 10 times, which looks like your code is hinting at:

for (int x= 0; x< 10; x++){

 for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}

16 Comments

ya but how will CN turn into 1 when it reaches 10 in the while example?
Because true can never be false, the while statement will repeat till the end of time(or until you close the app). Each time it repeats, it runs the for 1-10 again.
I got some errors. They are: expecting semicolon before CN (line 3), and expecting identifier before rightbrace (line 4). My code is: var CN:int = 1; while(true){ for (int CN=1; CN <= 10; CN ++) } NumberCounter.text = String(CN);
YOur code is different than what I posted. Please look at it again and you will see you have NumberCounter.text.... in the wrong place.
Ok I fixed my text and now all I get is expecting semicolon before CN (line 4)
|
0

Try this

if(CN>1) {

var CN:int = 1; for(int CN = 1; CN<10; CN++);

NumberCounter.text = String(CN); } else { }

Comments

0
var Numberofwins = 0;

CN.addEventListener(Event.ENTER_FRAME, checkFrame);

function checkFrame(event:Event):void{
if(CN.currentFrame == 11){
    CN.gotoAndPlay(1);
    }
}

    import flash.events.MouseEvent;

Submit.addEventListener(MouseEvent.CLICK, CheckIf8);

function CheckIf8(event:MouseEvent):void
{
    if(CN.currentFrame == 8) 
    {
            Numberofwins++;
        trace (Numberofwins);
        Scorebox.text = String(Numberofwins);
    }



    else
      {
    gotoAndStop("Loose1");
      }
}

This was my solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.