Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-21-2012, 09:43 AM   #1
BillyStickley7
Registered User
 
Join Date: May 2012
Posts: 1
Default [AS3] How to make an enemy follow the player?

I'm fairly new to flash and action script 3.0 but I've had a project to create maze game, I've made the maze on a layer, player on a layer, the decor on a layer and then an enemy, the player maze, and points system all work fine, but i have no idea how to implement the following of the player with an enemy and to keep the enemy within the maze like I've done with the player, the symbol is named enemy so if anyone could help me out how this to my code it would be appreciated this is my entire code:

ActionScript Code:
var rightArrow:Boolean = false;      var leftArrow:Boolean = false;     var upArrow:Boolean = false;     var downArrow:Boolean = false;         var dKey:Boolean = false;        var aKey:Boolean = false;     var wKey:Boolean = false;     var sKey:Boolean = false;         var speed :int = 5;     var coins :Array = [ coin1, coin2, coin3, coin4, coin5, coin6, coin7, coin8, coin9, specialCoin ];     var score :Number = 0;         stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);     stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);     stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);     function stage_onKeyDown(event:KeyboardEvent):void {         if(event.keyCode == Keyboard.RIGHT) rightArrow = true;         if(event.keyCode == Keyboard.LEFT) leftArrow = true;         if(event.keyCode == Keyboard.UP) upArrow = true;         if(event.keyCode == Keyboard.DOWN) downArrow = true;     }     function stage_onKeyUp(event:KeyboardEvent):void {         if(event.keyCode == Keyboard.RIGHT) rightArrow = false;         if(event.keyCode == Keyboard.LEFT) leftArrow = false;         if(event.keyCode == Keyboard.UP) upArrow = false;         if(event.keyCode == Keyboard.DOWN) downArrow = false;     }     function stage_onEnterFrame(event:Event):void {         var rect:Rectangle = player.getBounds(this);         var i:int = 0;         var xBump:int = 0;         var yBump:int = 0;         if(rightArrow) {             xBump = speed;             for(i = 0; i < speed; i++) {                 if(maze.hitTestPoint(rect.right + i, player.y, true)) {                     xBump = i - 1;                     break;                 }                 player.gotoAndStop ("Right");             }         }         if(leftArrow) {             xBump = -speed;             for(i = 0; i < speed; i++) {                 if(maze.hitTestPoint(rect.left - i, player.y, true)) {                     xBump = -i + 1;                     break;                 }                 player.gotoAndStop ("Left");             }         }         if(upArrow) {             yBump = -speed;             for(i = 0; i < speed; i++) {                 if(maze.hitTestPoint(player.x, rect.top - i, true)) {                     yBump = -i + 1;                     break;                 }                 player.gotoAndStop ("Up");             }         }         if(downArrow) {             yBump = speed;             for(i = 0; i < speed; i++) {                 if(maze.hitTestPoint(player.x, rect.bottom + i, true)) {                     yBump = i - 1;                     break;                 }                 player.gotoAndStop ("Down");             }         }         player.x += xBump;         player.y += yBump;                 for ( var c:int = 0; c < coins.length; c++)     {         if ( coins[c].visible &&             coins[c].hitTestObject( player )             )         {             if (coins[c] == specialCoin)             {                 score += 5;                 speed += 5; // power-up             }             else                 score++;                             coins[c].visible = false;             scoreText.text = "Score:" + score;                 }     }         }
BillyStickley7 is offline   Reply With Quote
Old 05-21-2012, 11:39 AM   #2
Scearezt
Satisfied User
 
Scearezt's Avatar
 
Join Date: Jun 2010
Location: C:\\System32\Hungary
Posts: 426
Lightbulb

Hey BillyStickley7,

Check out A* Pathfinding which is the best pathfinding way.
Scearezt is offline   Reply With Quote
Old 05-21-2012, 12:14 PM   #3
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,833
Send a message via Skype™ to henke37
Default

The only bad thing about it is that you can't use it if you don't have access to the actual map data. Which you don't if you draw the map in Flash.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 05-21-2012, 12:29 PM   #4
Scearezt
Satisfied User
 
Scearezt's Avatar
 
Join Date: Jun 2010
Location: C:\\System32\Hungary
Posts: 426
Lightbulb

How true, in this case, you can place some points, in order to know where's your character, and where to move your enemy.
Scearezt is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:32 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.