To define it for all 4 directions I think you would need 4 different collision free commands for each direction you are checking
For what I'm doing I only needed to check below the player to make sure there was something to walk on. To do this I used
if(CollisionFree("Event Actor",x,y+2))
So I'm checking to make sure there is no collision at the point 2 below the event actor.
In my code I have the player in different states. 2 of these are Walking and Falling
In the Walking State the player is able to walk as long as there is a platform underneath. When there is no platform the state changes to falling. When falling the players yvelocity is increased until there is a collision with a platform. This is done with a simple collision action with the platform which causes the yvelocity to = 0 and the state to go back to walking.
So my collision free section is simply
if(CollisionFree("Event Actor",x,y+2))
state = falling;
I will be posting the whole game sometime in the next couple of weeks. Just need to add all the enemy sprites and some kind of start and ending. However I can post the whole code for the section where I use the collisionfree command.
- Code: Select all
//In the draw Actor Action
char* key = GetKeyState();
switch (State)
{
case 1: //Walking
xspeed = (key[KEY_RIGHT] - key[KEY_LEFT]) * 5;
if (xspeed)
ChangeAnimationDirection("PlayerWan", FORWARD);
else
ChangeAnimationDirection("PlayerWan", STOPPED);
if ((xspeed > 0 && !WallRight)||(xspeed < 0 && !WallLeft))
x+= xspeed;
if(xspeed > 0 && PlayerWan.animindex !=0) ChangeAnimation("PlayerWan", "WanWalkRight", NO_CHANGE);
if(xspeed < 0 && PlayerWan.animindex !=1) ChangeAnimation("PlayerWan", "WanWalkLeft", NO_CHANGE);
if(xspeed == 0 && PlayerWan.animindex ==0 ) ChangeAnimation("PlayerWan", "WanFaceRight", NO_CHANGE);
if(xspeed == 0 &&PlayerWan.animindex ==1 ) ChangeAnimation("PlayerWan", "WanFaceLeft", NO_CHANGE);
CollisionState("PlayerWan", DISABLE);
if(CollisionFree("Event Actor",x,y+2))
State =2;
CollisionState("PlayerWan", ENABLE);
if (key[KEY_SPACE])
{
yvelocity -= 14;
State = Jumping
}
break;
case 2: //Falling
yvelocity++;
break;
case 3: //Jumping
if (yvelocity <18)
yvelocity+=1;
if ((xspeed > 0 && !WallRight)||(xspeed < 0 && !WallLeft))
x+= xspeed;
if(xspeed > 0)
{ if (PlayerWan.animindex !=0) ChangeAnimation("PlayerWan", "WanWalkRight", NO_CHANGE);
PlayerWan.animpos =1;
}
if(xspeed < 0)
{
if(PlayerWan.animindex !=1) ChangeAnimation("PlayerWan", "WanWalkLeft", NO_CHANGE);
ChangeAnimation("PlayerWan", "WanWalkLeft", NO_CHANGE);
PlayerWan.animpos =6;
}
ChangeAnimationDirection("PlayerWan", STOPPED);
break;
case 4: //dying
if (PlayerWan.lives <= 0)
{ State = Dead;}
else
{
RestartIn--;
if (RestartIn<=0)
{
CollisionState("Event Actor", ENABLE);
CollisionState("PlayerWan", ENABLE);
x = checkpointx;
y = checkpointy;
yvelocity = 0;
State = Walking;
ChangeAnimation("PlayerWan", "WanFaceLeft", FORWARD);
}
}
break;
case 5:
RestartIn--;
if (RestartIn<=0)
LoadGame("WanDed.ged");
break;
}
Hope this helps.