## As: Platformer Ai (on Slopes)

• 5,242 Views
• 15 Replies
Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
As: Platformer Ai (on Slopes) 2006-07-01 12:51:06

AS: Movement on Slopes - pyro 111

AS: Main XD

THE TUTORIAL

First, create your character and platforms using this tutorial: AS: Movement on Slopes - pyro 111

Give your character the instance name "circle" without the quotes

Now, make your enemy movie clip. It must not be instance named.

Put two frames in your enemy movie clip. MAKE SURE THE ENEMY IS FACING LEFT!!!!!!
Frame 1 will be walking, and you should be able to work out how to make it attack (if you want that) I will only be teaching you how to script the walking, and here it is:

// original script by pyro111
// AI edited by Hoeloe
grav = 0;
// sets the gravity
speed = 4;
// sets movement speed
scale = _xscale;
// sets the size of the enemy
}
onClipEvent (enterFrame) {
// happens every frame
grav++;
// makes you fall faster
_y += grav;
// makes your _y go up by the varaible grav
while (_root.ground.hitTest(_x, _y, true)) {
// when this is touching the ground
_y--;
// makes the y keep going up, this means you can walk on slopes XD
grav = 0;
// and you wont fall, another XD
}
if(_xscale == scale){
// if your _xscale is the same as the variable scale (facing the start direction)
this._x -= speed;// moves the enemy left by speed
}
if(_xscale == -scale){// if your _xscale is facing the opposite way from the start
this._x += speed;// moves right by speed
}
if (!_root.ground.hitTest(_x, _y+5, true)) {
// if you are touching a big enough drop
if (_xscale == scale){
// if you are facing left
this._x += speed;
// you move back by speed
this._xscale = -scale;
// and change direction
}else if (_xscale == -scale){
// if you are facing right
this._x -= speed;
// you move back by speed
this._xscale = scale;
// and turn around
}
}
if (_root.ground.hitTest(_x, _y-5, true)) {
// if you hit a wall
if (_xscale == scale){
// if you are facing left
this._x += speed;
// you move back by speed
this._xscale = -scale;
// and change direction
}else if (_xscale == -scale){
// if you are facing right
this._x -= speed;
// you move back by speed
this._xscale = scale;
// and turn around
}
}
}

Please notify me of any problems

This script will work for flash 7, im not sure about any of the others

What this should look like:

DEMO

I hope this helped, please leave feedback

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Depredation
Depredation
• Member since: Sep. 5, 2005
• Offline.
Forum Stats
Member
Level 17
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-07-01 15:52:00

Its nice but abit simple. Mabye expand it onto attacking and such.

CreateAndDestroy
CreateAndDestroy
• Member since: Dec. 15, 2003
• Offline.
Forum Stats
Member
Level 21
Response to As: Platformer Ai (on Slopes) 2006-07-01 16:21:35

cool

m4x0
m4x0
• Member since: Nov. 15, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Platformer Ai (on Slopes) 2006-07-01 17:26:26

attacking:

Create a variable called lives

lives = 3;
if (_root.hero.hitTest(_root.enemy)){
_root.lives == _root.lives - 1;
if (_root.lives == 0 gotoAndStop ("gameover"))
}

Not sure, think that will work.

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-07-02 05:12:07

no that wont work

because then you can only have one enemy

you have to refer to the enemy as "this." and put the script on the enemy

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-07-02 05:14:17

so the script would be:

ON THE FRAME:
lives = 3;
onEnterFrame = function(){
if(lives <= 0){
gotoAndStop("gameover");
}
}

ON THE ENEMY:
onClipEvent(enterFrame){
if (_root.circle.hitTest(this)){
_root.lives--;
}
}

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-07-02 05:16:37

actually, i forgot something, that previous script will make it so that every frame you are touching the enemy, you lose a life! you will need to hold a true/false variable to check if the player is touching the enemy.

onClipEvent(enterFrame){
if(this.hitTest(_root.circle) && !hit){
_root.lives --;
hit = true;
}
if(!this.hitTest(_root.circle)){
hit = false;
}
}

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-07-02 05:19:11

but, that is not really attacking, that is just being hurt, if you want attacking, make the second frame of your enemy the image of attacking, and add the previous script to the attacking movie clip.

then, add this script to the enemy:

onClipEvent(enterFrame){
if(this.hitTest(_root.circle) && _currentframe == 1){
gotoAndStop(2);
} else{
gotoAndStop(1);
}
}

~The above script has not been tested~

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Pyromaniac
Pyromaniac
• Member since: Jan. 14, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Platformer Ai (on Slopes) 2006-07-02 15:06:05

Yey I feel special that my script was used.

That is a nice edit of my script, and will help people out. Good job. Unfortunatly, I updated the script I made to make it much less buggy and better, so I guess ill post the newest script on my thread.

Majestic-Boy
Majestic-Boy
• Member since: Jun. 27, 2006
• Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Platformer Ai (on Slopes) 2006-07-30 05:29:21

Pardon me for sounding noobish. But I wanted to make some moving landspace on the air, so that my character can jump on it. Just like in mario.

So i was thinking on how to do it.. is it possible to use your enemy script for the movements of the landspace? it doesn't attack. I just want my character to stand on it. And when the landspace moves, my character goes with it.

I initially tried animating the movement. Which of course, IS WRONG. silly me. When the landspace moves, my character standing on top is left. Not riding with the land.

lol. Any suggestions on how to do it? Thanks =)

Vengeance
Vengeance
• Member since: Mar. 18, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Platformer Ai (on Slopes) 2006-07-30 05:43:39

What is it with people continually trying to make tutorials on specific things that have been covered in tutorials on AS:Main?
Using pyros script and adding some blatant obvious code that anyone who had searched AS:Main for, adding it together and posting a tutorial on it does not count as a tutorial.
Proper tutorials should leave you with around 1000 characters left.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2006-08-14 08:43:00

At 7/30/06 05:29 AM, Deep_Moogle wrote:
lol. Any suggestions on how to do it? Thanks =)

I do have a suggestion. If your platform is animated, it will need the instance name plat:

onClipEvent(enterFrame){
if(_root.plat._currentframe < theframeitchangesdirectionat){
_x -= speedofplatform // if the enemygoes the wrong way, change - to +
}else{
_x += speedofplatform // same
}
}

If the platform is scripted, change the current frame to _x and put in which _x point
it changes direction at, hope you can understand that, im sometimes pretty unclear...

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to As: Platformer Ai (on Slopes) 2006-08-14 09:22:25

Awww Dammit! I was going to do a tutorial on this TODAY, except with enemy AI interacting with the player... Good job, quite simple, but good. I wish you went on to talk about how to make AI even better by giving it random speeds, interaction with player, and other ground objects, and what not.

Still, It's good and I'm happy you made it ;)

Hoeloe
Hoeloe
• Member since: Apr. 29, 2004
• Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Platformer Ai (on Slopes) 2007-02-08 14:20:53

Random speeds is not practical. It gives for an unbalanced game and the feeling of sloppy script, a pre-set speed feels much better when actually playing a platformer game...

Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

Lochie
Lochie
• Member since: Feb. 17, 2005
• Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Platformer Ai (on Slopes) 2007-07-02 12:32:21

Okay I have the enemy up and running and all, but I have to put the enemy into an MC for my game.

MC>ENEMY(WITH SCRIPT)

Now I have no idea what I should change to get it working.

Anybody?

asdfg

sandypaw
sandypaw
• Member since: Jun. 28, 2007
• Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Platformer Ai (on Slopes) 2007-07-28 13:19:38

how would i make an end point? when i hit a guy it sends me to frame 3? or something like that

=3