A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: square chasing square

  1. #1

    square chasing square

    Ok, I have this game, and some levels have a moving square as the player, you move the square with your arrow keys....

    Ok, so I want to have an enemy square that chases the player square....

    Anyone remember the ghosts in mario? They only chased mario when you didn't look at them? Kinda like that...... but in this regards:

    I only want the enemy square to chase the player square when the player square moves.... soo say the player square moves 5 pixels per movement (know how to do all that), I want the enemy square to move when the player square moves..... towards the player square at like 7 pixels per movement..... so eventually you won't be able to avoid the enemy square....

    Any ideas?

    This would be GREATLY appreciated, thanks so much!

    -Clarence

  2. #2
    Here's a link:

    http://www.levelgame.net/test306.html

    I want the evil 666 square to move towards the player only when the player moves....

    Like I said, and ideas would seriously help me.

    -Clarence

  3. #3
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    Code:
    playerSpeed=5;
    enemySpeed=7;
    onEnterFrame=function(){
    	if(Key.isDown(Key.RIGHT)){
    		player._x+=playerSpeed;
    		advanceEnemy();
    	}else if(Key.isDown(Key.LEFT)){
    		player._x-=playerSpeed;
    		advanceEnemy();
    	}else if(Key.isDown(Key.DOWN)){
    		player._y+=playerSpeed;
    		advanceEnemy();
    	}else if(Key.isDown(Key.UP)){
    		player._y-=playerSpeed;
    		advanceEnemy();
    	}
    }
    function advanceEnemy(){
    	//move enemy toward player
    	theta=Math.atan2(player._y-enemy._y,player._x-enemy._x);
    	enemy._x+=enemySpeed*Math.cos(theta);
    	enemy._y+=enemySpeed*Math.sin(theta);
    	if(enemy.hitTest(player)){
    		//the player was caught by the enemy
    		trace("hit!");
    	}
    }
    Z¡µµ¥ D££

    Soup In A Box

  4. #4
    OMG thank you so much! I will have to try this later, and if this works I will seriously praise your name!

  5. #5
    BTW_ this worked, you are my hero! No joke!

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