dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Increasing variables in a maze game

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    9

    Increasing variables in a maze game

    Hi

    I am trying to create a simple(ish) maze game. I have the basics of the character staying in the maze area and getting him to the finish. Now I want to put some icons in the maze which the character has to rollover (collect) before he can complete the level.

    So basically I presume I have to create a variable starting at 0 and increases by one when you rollover the icon. When you reach the desired number then you can exit the maze, not before.

    Here is the code I have on the character:

    //
    // CHARACTER VARIABLES FOR SPEED AND SIZE.
    //
    onClipEvent (load) {
    // Speed which you will move round the maze.
    speed = 10;
    // Size of the character so it stays in the maze.
    radius = 15;
    }

    //
    // CONTROL WHAT THE CHARACTER DOES.
    //
    onClipEvent (enterFrame) {

    // MOVE THE CHARACTER USING ARROW KEYS
    // Move Left.
    if (Key.isDown(Key.LEFT)) {
    _x -= speed;
    }
    // Move Right.
    if (Key.isDown(Key.RIGHT)) {
    _x += speed;
    }
    // Move Up.
    if (Key.isDown(Key.UP)) {
    _y -= speed;
    }
    // Move Down.
    if (Key.isDown(Key.DOWN)) {
    _y += speed;
    }

    // KEEP THE CHARACTER INSIDE THE WALLS.
    while (_root.maze_mc.hitTest (_x, _y + radius, true)) {
    _y --;
    }
    while (_root.maze_mc.hitTest (_x, _y - radius, true)) {
    _y ++;
    }
    while (_root.maze_mc.hitTest (_x - radius, _y, true)) {
    _x ++;
    }
    while (_root.maze_mc.hitTest (_x + radius, _y, true)) {
    _x --;
    }

    // WHEN YOU REACH THE END OF THE MAZE
    if ((_root.finishLine.hitTest(_x, _y + radius, true)) or (_root.finishLine.hitTest(_x, _y - radius, true)) or (_root.finishLine.hitTest(_x - radius, _y, true)) or (_root.finishLine.hitTest(_x + radius, _y, true))) {
    trace ("Maze Complete");
    }
    }

    Here is the code I have on the icon:

    //
    // DETECT TO SEE IF THE CHARACTER IS OVER THE HIT AREA.
    //
    onClipEvent (enterFrame) {
    if (this.hitTest (this._parent.character_mc)) {

    // The Character is in the Hit Area.
    trace ("Character in Hit Icon 01");

    } else {
    trace ("");
    }
    }



    Can anyone help on how this can be done. If anymore information is needed please let me know.

    Thanks

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You should keep the counter attached to the character so that the item just has to worry about incrementing the number and getting rid of itself.

    Code:
    //on the character's onClipEvent(load){} add this:
    totalItems = 10; //some arbitrary number
    collectedItems = 0; //total collected defaults to 0
    then on the items hitTest, add this:
    Code:
    if (this.hitTest (this._parent.character_mc)) {
    // The Character is in the Hit Area.
      trace ("Character in Hit Icon 01");
      this._parent.character_mc.collectedItems++; //add 1 to collectedItems
      
    } else {
      trace ("not hitting");
    }
    Then later where you test against the exit, add a condition that checks collected against total

    Code:
    // WHEN YOU REACH THE END OF THE MAZE
    if ((_root.finishLine.hitTest(_x, _y + radius, true)) or (_root.finishLine.hitTest(_x, _y - radius, true)) or (_root.finishLine.hitTest(_x - radius, _y, true)) or (_root.finishLine.hitTest(_x + radius, _y, true))) {
      if(collectedItems >= totalItems){
        trace ("Maze Complete");
      }else{
        trace("All items not collected);
      }
    
    }
    }

  3. #3
    Junior Member
    Join Date
    Jul 2007
    Posts
    9
    Thats great and works just as I want. One other question though which I didn't think about, the hit test is updating on enter frame, which makes my collected items increase every frame. Is there a way to stop the collected items increasing on the object, ie, make the object disappear so you have to progress round the maze to get the other nine?

    Does this make sense?

    Thanks again.

  4. #4
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You could write some complicated tracking that would track a list of the items by name and only add it to the obtained list if it is not already added. Just a matter of tracking two arrays.

    If that sounds too tough for now you can likely get away with just removing the item as soon as it's touched so that the hitTest only gets once chance to run.

    If you are adding the items at author time, they'll be at a negative depth at runtime. To remove movieclips added at author time, you'll have to move it to a positive depth via swapDepths()

    add this to the hitTest's success condition
    Code:
    if (this.hitTest (this._parent.character_mc)) {
    // The Character is in the Hit Area.
      trace ("Character in Hit Icon 01");
      this._parent.character_mc.collectedItems++; //add 1 to collectedItems
      this.swapDepths(this._parent.getNextHighestDepth());
      removeMovieClip(this);
      
    } else {
      trace ("not hitting");
    }
    or the easiest but dirtiest, set the items _visible to false

    Code:
    if (this.hitTest (this._parent.character_mc)) {
    // The Character is in the Hit Area.
      trace ("Character in Hit Icon 01");
      this._parent.character_mc.collectedItems++; //add 1 to collectedItems
      this._visible = false;
      
    } else {
      trace ("not hitting");
    }
    Last edited by jAQUAN; 05-25-2008 at 02:31 PM.

  5. #5
    Junior Member
    Join Date
    Jul 2007
    Posts
    9
    I'll have a look at the complicated way. In the meantime the other option works great and is just what I need.

    Thanks again for you help, I appreciate it.

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