I miss spelled collision, I feel smart.
So here is a long one.
I need help with a better way to have sprites in my program bumping into each other.
As of writing I have discovered the commands dbSpriteWidth and dbSpriteHeight so I do not know if those are of any use yet.
My two main goals is to have workable "walls" and "platforms" walls seem easier, but with platforms, I can not figure out how to make the sprite to return to its original location on the "ground level". Nor will the jump function I have want to recognize its new location.
The walls themselves I feel like I cheated because all I have it doing is when the player sprite comes into contact with a wall sprite, it will simply push him back one more then his movement speed, making it of course impossible to pass, but has this obvious look about it, as you hold down the button, they sprite will effectively "rock" back and forth against the "wall", which looks too fake. Strangly enough if I put it as the same value as speed, the player will still be able to pass through it.
The platforms right now simply move me up the Yaxis. I of course stay there since I haven’t made a type of gravity that is constantly going on. I do not know how I would do this.
When the sprites are created, they actual locations where they seem to “activate” is far away from the actual image of the sprite, its not even centered, the player sprite might move half way through the “wall” sprite for actually being pushed back.
int cLeft = 200;
int cRight = 300;
int cUp = 400;
int cDown = 500;
void setupColision ( void )
{
dbLoadImage ( "Assets\Fbolt.png" , 99 );
dbLoadImage ( "Assets\Wbolt.png" , 100 );
dbSprite ( cLeft, 400, 400, 100 );
dbSetSpritePriority ( cLeft , 1 );
dbSprite ( cRight, 402, 400, 100 );
dbSetSpritePriority ( cRight , 1 );
dbSprite ( cUp, 300, 400, 99 );
dbSetSpritePriority ( cUp , 1 );
dbSprite ( cDown, 250, 400, 99 );
dbSetSpritePriority ( cDown , 1 );
}
void Colision ( void )
{
if (dbSpriteX(1) == dbSpriteX(cLeft) && dbSpriteY(1) == dbSpriteY(cLeft))
{
iPlayerX = iPlayerX - 6;
}
if (dbSpriteX(1) == dbSpriteX(cRight) && dbSpriteY(1) == dbSpriteY(cRight))
{
iPlayerX = iPlayerX + 6;
}
if (dbSpriteX(1) == dbSpriteX(cUp) && dbSpriteY(1) == dbSpriteY(cUp))
{
iPlayerY = iPlayerY + 30;
groundLevel = dbSpriteY(cUp);
}
if (dbSpriteX(1) == dbSpriteX(cDown) && dbSpriteY(1) == dbSpriteY(cDown))
{
iPlayerY = iPlayerY - 30;
groundLevel = dbSpriteY(cDown);
}
}
Player.cpp
#include "DarkSDK.h"
#include "Player.h"
#include "Input.h"
#include "Colision.h"
// player variables
// Player X position
int iPlayerX = 200;
// player Y position
int iPlayerY = 400;
// speed the player should move
int iPlayerSpeed = 2;
int iPlayerFrame = 2;
bool bPlayerIsLeft = false;
// track how left the player is going
bool bPlayerIsRight = true;
// track how right the player is going
int groundLevel = 400;
//should change where he is walking
int playerdies = 0;
bool inAir=false;
bool Jumping = false;
int StartingSpeed = -10;
int Speed = -5; // whatever, this will be initialized in the code below
int Gravity = -1; // the thing that makes you fall
// player directions
const int STRAIGHT = 0 , LEFT = 1 , RIGHT = 2;
int iPlayerDirection = STRAIGHT;
// set up the animated sprite for the player and load in the explosion images as well as lazer bolt and the icon for a lives display
void playerSetup ( void )
{
// the player animated sprite
dbCreateAnimatedSprite ( 1 , "Cronoo.bmp", 6, 3, 1 );
dbSprite ( 1 , iPlayerX , iPlayerY , 1 );
dbSetSpriteFrame ( 1 , iPlayerFrame );
dbSetSpritePriority ( 1 , 2 );
dbLoadImage ( "Assets\bolt.png" , 2 );
// ship icon to use for showing how many lives we have left
dbLoadImage ( "Assets\shipicon.png" , 40 );
}
// update the player in game
void playerUpdate ( void )
{
// see if the player needs to try and go left
if ( checkLeft() )
{
iPlayerX -= iPlayerSpeed;
if ( iPlayerX < 0 )
iPlayerX = 0;
iPlayerDirection = LEFT;
}
// see if the player needs to try and go right
else if ( checkRight() )
{
iPlayerX += iPlayerSpeed ;
if ( iPlayerX > 1024 - 128 )
iPlayerX = 1024 - 128;
iPlayerDirection = RIGHT;
}
// not left or right, so lets face straight
else
iPlayerDirection = STRAIGHT;
if (dbUpKey() && ! Jumping) //JUMPING COMMAND HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
Jumping = true;
Speed = StartingSpeed;
//dbCreateAnimatedSprite ( 1, "Cronojump.bmp", 1, 4, 90 );
//dbDeleteSprite ( 1 ); Why was this here
}
if (Jumping)
{
iPlayerY += Speed;
Speed -= Gravity;
// check if we are back on ground
if (iPlayerY >= groundLevel)
{
iPlayerY = groundLevel; // just to be safe, position sprite at ground level
Jumping = false; // stop jumping
}
}
// animate the player
// place the player sprite at it's correct location
dbSprite ( 1 , iPlayerX , iPlayerY , 1 );
// set the frame the player sprite should show
dbSetSpriteFrame ( 1 , iPlayerFrame );
}
Input.cpp
#include "DarkSDK.h"
#include "Input.h"
// check the keys we have assigned to be the left key
bool checkLeft ( void )
{
if ( dbKeyState ( 203 ) || dbKeyState ( 30 ) )
return true;
return false;
}
// check the keys we have assigned to be the right key
bool checkRight ( void )
{
if ( dbKeyState ( 205 ) || dbKeyState ( 32 ) )
return true;
return false;
}
bool checkJump ( void )
{
if( dbKeyState(31) || dbKeyState(206))
return true;
return false;
}
Main.cpp
#include "DarkGDK.h"
#include "Player.h"
#include "Input.h"
#include "Colision.h"
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbDisableEscapeKey ( );
dbRandomize ( dbTimer ( ) );
dbLoadImage ( "backdrop.bmp", 1000 );
dbSprite ( 1000, 0, 0, 1000 );
dbSetImageColorKey ( 255, 0, 255 );
playerSetup();
setupColision();
while ( LoopGDK ( ) )
{
playerUpdate ();
Colision ();
if ( dbEscapeKey ( ) )
break;
// here we make a call to update the contents of the screen
dbSync ( );
}
return;
}
Player.h
#pragma once
void playerSetup ( void );
void playerUpdate ( void );
extern int iPlayerX;
extern int iPlayerY;
extern int iPlayerSpeed;
extern int iPlayerFrame;
extern int iFrameAnimateDelay;
extern bool inAir;
extern bool Jumping;
extern int StartingSpeed;
extern int Speed; // whatever, this will be initialized in the code below
extern int Gravity;
extern int groundLevel;
Input.h
#pragma once
bool checkFire ( void );
bool checkLeft ( void );
bool checkRight ( void );
bool checkJump ( void );
Colision
#pragma once
void setupColision ( void );
void Colision ( void );
extern int cLeft;
extern int cRight;
extern int cUp;
extern int cDown;