Enemy code. Note: for this to work, the main character must have the instance name "layer".
onClipEvent (enterFrame) { if (_root.player._x > _x) { _x += 5; }}onClipEvent (enterFrame) { if (_root.player._x<_x) { _x -= 5; }}onClipEvent (enterFrame) { if (_root.player._y>_y) { _y += 5; }}onClipEvent (enterFrame) { if (_root.player._y<_y) { _y -= 5; }}
// Initializing Variables var MAX_HEALTH:Number = 25; var charHealth:Number = MAX_HEALTH; var enemyDamage:Number = 5; var healthIncrement:Number = 10; // Check hit and apply damage if (_root.character.hitTest(_root.enemy)) { if (charHealth > 0){ charHealth -= enemyDamage; } else { // Code for whatever happens when the character dies. } } // If you want to cure the character (after all that's the only purpose I see for having a maxHealth) if (_root.character.hitTest(_root.magicalCurePill)) { charHealth += healthIncrement; if (charHealth > MAX_HEALTH) charHealth = MAX_HEALTH; }
A quick comment on (at least) the enemy code... I see you've used 'onClipEvent(enterFrame)' at least four times... there's no need for that If it executes one of the _x functions, the other one doesn't need to... it can be put into one simple function... onClipEvent(enterFrame){ if(player_x > x1){ x -=5; } else if(player_x < x2){ x +=5; } if(player_y > y1){ y -=5; } else if(player_y < y2){ y +=5; } }...like that. I see you've done this a couple of times, but keeping it simple and readable should be priority #1... it certainly doesn't help if you get lost in your own code!
Other than that, I can just say try it out! You learn the most from fixing the most irritating errors you yourself made!