A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Help with a variable

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11

    Unhappy Help with a variable

    Okay I am working in flash Cs3, working on designing a game. I have the basic workings down but I cannot seem to get my variables to set more then one value (i=1, i<200, ++i).

    this is the code on the hit box around the character, and it is supposed to stop him when he hits walls. However, the only instance name that works, within the ["wall"+i] is wall1.

    Actionscript Code:
    onClipEvent (load) {
        stop();
        [B]i = (i=1, i<200, i++);[/B]

    }
    onClipEvent (enterFrame) {


        if (hitTest(_root["wall"+i])) {
            if (Key.isDown(Key.UP)) {
                _root["wall"+i]._y -= 10;
                _root["roof"+i]._y -= 10;
            }
            if (Key.isDown(key.RIGHT)) {
                _root["wall"+i]._x += 10;
                _root["roof"+i]._x += 10;
            }
            if (Key.isDown(key.LEFT)) {
                _root["wall"+i]._x -= 10;
                _root["roof"+i]._x -= 10;
            }
            if (Key.isDown(key.DOWN)) {
                _root["wall"+i]._y += 10;
                _root["roof"+i]._y += 10;
            }
        }
    }

    The bold is where I seem to be having my problem, I have set the value of i, but when i actually use it, the only value that seems to be initialized is i having a value of 1. does anyone know how i might be able to fix this and or another way I can make a hit test for the walls that will not need a seperate string of coding for each individual wall?

    thank you in advance.
    if you need anything else just ask.

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    that's only the statement part of a for loop, you miss the for loop itself. And the use of i must be inside the for loop handler. See more info here.

    gparis

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Ok, I couldn't get a for loop to work within my on(enterframe) but i got it to work now. new problem.. :S

    okay so i change it to :

    Actionscript Code:
    onClipEvent (enterFrame) {

        for (i=0; i<200; ++i) {
            if (hitTest(_root["wall"+i])) {
                if (Key.isDown(Key.UP)) {
                    _root["wall"+i]._y -= 10;
                    _root["roof"+i]._y -= 10;
                }...etc

    Now Multiple walls are being able to be hit, but when i hit one, everything else (the secnd wall, and the roof) continue to move as if i'm not hitting anything. Both walls work individually, but only one at a time. :S

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    You'd want to verify, as an extra condition, that the last digit of the instance name matches i. You could use a substring. As in :

    actionscript Code:
    if (i==Number(_root["wall"+i]._name.substring(4)))

    gparis

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    I'm sorry i don't quite understand what you mean, i've never done that kind of thing. :S

    I don't know if this helps but the variable seems to be working good, any value of "i" seems to being caught on the hittest, below 200 of course.
    It's just that only one value seems to be working.

    If I hit a wall, that wall stops, but all other walls and the roof continue to move.
    But if i hit the other walls they stop and the others which where working before no longer do.

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Okay so my problem exactly is that when i perform a hit test (run into the wall), it freezes the value of i to that of the current hittest.
    This meaning that if I am hittesting a wall named (wall10) then the only other object i can get to stop is another object named (roof10)

  7. #7
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    That's what the extra condition i posted is for. the substring will retrieve the number 10 and only wall10 and/or roof10 will be affected.

    gparis

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Oh ok. I don't mean to a bother, but i only started deep into action script this year. Where exactly do i put that into the code to make it work?

    I tried it in a few places but it made no difference. :S

    I'm thinking I probably have to add in more then just that line of code, but i'm completely lost with it.

    A reply asap would be nice, i'm working on this at school and my teacher is getting *****y due to my lack
    of completing anything.
    I tried searching the internet for answers, but nothing seemed to come up that was relevant.
    Last edited by JoshuaMack; 04-19-2010 at 12:16 PM.

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Nevermind, I got everything to work, thanks anyways.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center