• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Collision/changing movement direction

K

Kramer

Guest
I'm working on a project and I cannot figure out this last step. What I need to do is when birdrightobj gets low health, it retreats to nestobj, restores myhealth to 100, and then goes back into attack mode. The retreat part works, but myhealth does not go to 100 and birdrightobj is stuck to the nest. This is the code I am having trouble with:

if(NPCState == "retreat")
{
path_end()
nearnest = instance_nearest(x,y,nestobj)
if( x==nearnest.x && y==nearnest.y)
{
speed=0
myhealth=100
NPCState="attack"
}
else
{
direction = point_direction(x,y,nearnest.x, nearnest.y)
speed=5
}
}
 
Last edited by a moderator:
K

Kramer

Guest
This is my entire code for birdrightobj step event:

if( myhealth<=0)
dead = true

if ( collision_point(x,y,starfishobj,true,true) )
{
starfishcollide=true
myhealth=myhealth-30
with(starfishobj)
{
instance_destroy()
}
}

// check for proximity
if( distance_to_object(crabobj) < closetrigger)
nexttome=true

// check for start/end of path
if( NPCState=="patrol" && path_index!= -1 )
{
if( path_position<=.01 && image_xscale==-1)
{
image_xscale=1
// GML 2.0 code
path_end()
path_start(birdpath,4,3,0)
////////////////

}
if( path_position >= .99 && image_xscale==1)
{
image_xscale=-1
}
}

// check for low health and set flag
if( myhealth <= 20)
lowhealth=true
// Part B //////////////////////////////////////////////////////////
// Update NPCState
////////////////////////////////////////////////////////////////////
if((starfishcollide || nexttome)&&NPCState=="patrol")
NPCState="attack"
if( NPCState=="attack" && lowhealth)
NPCState="retreat"

if( NPCState=="attack" || NPCState=="retreat" )
if( dead==true)
{
NPCState="dead"
sprite_index=deadbirdsp
speed=0
}

// Part C ///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Run State based on value of NPCState variable
// dead? then don't do anything
if(NPCState=="dead")
{
exit;
}
if(NPCState == "attack")
{
path_end()
direction = point_direction(x,y,crabobj.x,crabobj.y)
speed=3
if( distance_to_object(crabobj) < .01)
speed=0
//b = instance_create_layer(x,y,”instances”,objbullet) // gamemaker studio 2.0
if(!instance_exists(eggobj))
{
// b = instance_create(x,y,eggobj)
b = instance_create_layer(x,y,"instances",eggobj)
ammo=ammo-1
with(b)
{
direction = point_direction(x,y,crabobj.x,crabobj.y)
speed = 5
}
}
}
if(NPCState == "retreat")
{
path_end()
nearnest = instance_nearest(x,y,nestobj)
if( x==nearnest.x && y==nearnest.y)
{
speed=0
myhealth=100
NPCState="attack"
}
else
{
direction = point_direction(x,y,nearnest.x, nearnest.y)
speed=5
}
}
 
Last edited by a moderator:

woods

Member
maybe something like this.. would reset your health to 100 when touching the nest?

Code:
if(NPCState == "retreat")
{
path_end()
nearnest = instance_nearest(x,y,nestobj)
if place_meeting( x,y,nestobj)
{
speed=0
myhealth=100
NPCState="attack"
}
else.....
 
Top