A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [F8] Problem with hitTest when having Two Walls

  1. #1
    1337Phantom-I
    Join Date
    Jun 2008
    Location
    My World!
    Posts
    25

    Question [F8] Problem with hitTest when having Two Walls

    I was working on a flash (8) project and I got stuck with the walls.

    I made a moving character with 4 walls surrounding him; the walls work fine though. The problem is I wanted to make a 5th wall in the middle simply by duplicating the right wall or making another wall with the same code as the right wall. Which for some reason doesn't work; all the walls work fine except the 5th one which I am able to go through.

    Here's the code for the 4 walls:


    onClipEvent (enterFrame) {
    if (hitTest(_root.character)) {
    _root.left = false;
    } else {
    _root.left = true;
    }
    }


    onClipEvent (enterFrame) {
    if (hitTest(_root.character)) {
    _root.up = false;
    } else {
    _root.up = true;
    }
    }


    onClipEvent (enterFrame) {
    if (hitTest(_root.character)) {
    _root.right = false;
    } else {
    _root.right = true;
    }
    }


    onClipEvent (enterFrame) {
    if (hitTest(_root.character)) {
    _root.down = false;
    } else {
    _root.down = true;
    }
    }




    Thanks for taking your time to read this...

  2. #2
    Intermediate Game Dev pseudobot's Avatar
    Join Date
    May 2008
    Location
    New Zealand
    Posts
    561
    Wouldn't it be easier to just have something like this:

    Code:
    onClipEvent(enterFrame)
    {
    if (this.hitTest(_root.character) == true)
    {
    _root.character._x += 10  //as long as it is on the left-ish side. Note that the 
                                        //10 has to be higher then the characters normal           
                                        //speed, else it goes through the walls.
    }
    }
    I think that should work...just remember to change the _root.character._x to something like _root.character._y if the wall goes across.

  3. #3
    1337Phantom-I
    Join Date
    Jun 2008
    Location
    My World!
    Posts
    25

    Question Character Code

    Well that didn't really work; perhaps I should give you the code of the character:

    Code:
    onClipEvent (load) {
    	root.up
    	root.down
    	root.left
    	root.right
    	
    	XSpeed = 6;
    }
    
    onClipEvent (enterFrame) {
    	if (Key.isDown(87) && _root.up == true) {
    		_rotation = 270;
    		_y-= XSpeed;
    	}
    	if (Key.isDown(83) && _root.down == true) {
    		_rotation = 90;
    		_y+= XSpeed;
    	}
    	if (Key.isDown(65) && _root.left == true) {
    		_rotation = 180;
    		_x-= XSpeed;
    	}
    	if (Key.isDown(68) && _root.right == true) {
    		_rotation = 0;
    		_x+= XSpeed;
    	}
    }
    Last edited by 1337Phantom-I; 08-01-2008 at 08:46 PM.

  4. #4
    Working On An Online RPG grimm 88's Avatar
    Join Date
    Sep 2007
    Location
    Nowhere
    Posts
    280
    the reason your fifth wall is not working is because you are using _root.left = true and false, but if you are using the same value on two different walls it will change both of the walls values, so because both walls can never touch the character at the same time, whenever you hit left wall number 1, it will try and make _root.left = false, but then because the other wall is not touching the character, it will make _root.left = true and overwrite the false so you will be able to walk through the wall

    in short... find another method rather than using true's and false's since sooner or later you will run into problems... in your case, its sooner

    pseudobot's code will work, as long as it is implemented correctly like so...

    character code:
    Code:
    onClipEvent (load) {
    	XSpeed = 6;
    }
    onClipEvent (enterFrame) {
    	if (Key.isDown(87)) {
    		_rotation = 270;
    		_y-= XSpeed;
    	}
    	if (Key.isDown(83)) {
    		_rotation = 90;
    		_y+= XSpeed;
    	}
    	if (Key.isDown(65)) {
    		_rotation = 180;
    		_x-= XSpeed;
    	}
    	if (Key.isDown(68)) {
    		_rotation = 0;
    		_x+= XSpeed;
    	}
    }
    wall code (in this case left, edit for other walls):
    Code:
    onClipEvent(enterFrame)
    {
    if (this.hitTest(_root.character) == true)
    {
    _root.character._x += _root.character.XSpeed
    //it CAN be exactly equal to the character's speed
    }
    }

  5. #5
    1337Phantom-I
    Join Date
    Jun 2008
    Location
    My World!
    Posts
    25
    TY the code works great; although I did ran into another problem along the way with the walls. I want my character to not be able to go through the "fifth wall" from neither sides (right,left,top and bottom). The problem is when I put the code for all sides into the wall; it starts to have complications...

  6. #6
    Working On An Online RPG grimm 88's Avatar
    Join Date
    Sep 2007
    Location
    Nowhere
    Posts
    280
    yeah, it would because its trying to move the character in 4 directions that all cancel out... it would kinda spasm

    to fix that you can have 4 separate movieclips that make up your wall (thats how i would have done it years ago, but now i am wondering if there is a better way)

    anyway, if i figure something else out i will keep you posted, otherwise its 4 movieclips im afraid

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