I have some code where a tank is positioned at the screen and the tank will move to the location you specify by the right mouse click, the tank is a class called Tank. However i've run into a weird problem, when i load the tank outside of the while loop (before the while loop) everything works fine, but if i load the object inside the while loop, the object will only move when you hold your right mouse button in, (it will not move to the specified location unless you hold in your right mouse click all the time).
This is the piece of the with the object loading outside of the while loop.
The problematic code is marked with stars.
#include "DarkGDK.h"
#include <string>
using namespace std;
// the main entry point for the application is this function
int n = 1;
class Tank
{
public:
float name;
float speed;
float rotateSpeed;
float damage;
float health;
float xHit;
float yHit;
float deltaX;
float deltaY;
float targetAngle;
bool targetPlaced;
float rotate;
bool rotated;
float targetName;
float map;
Tank()
{
name = n;
map = 10000;
speed = 3;
rotateSpeed = 0.5;
damage = 20;
health = 100;
targetPlaced = false;
rotated = false;
}
void load()
{
dbLoadObject("tank2.x", n);
dbRotateObject(n, -90, 0, 0);
dbScaleObject(n, 1000, 1000, 1000);
n++;
}
void move()
{
if (dbPickObject(dbMouseX(), dbMouseY(), name, name) == name)
{
dbText(dbMouseX(), dbMouseY(), "Targeted");
if (dbMouseClick() == 1)
{
targetName = name;
}
}
if (targetName == name)
{
if (dbMouseClick() == 2)
{
dbPickObject( dbMouseX (), dbMouseY (), map, map );
xHit = dbGetPickVectorX() + dbCameraPositionX();
yHit = dbGetPickVectorY() + dbCameraPositionY();
deltaX = xHit - dbObjectPositionX(name);
deltaY = yHit - dbObjectPositionY(name);
targetAngle = dbATANFULL(deltaX, deltaY);
if (targetAngle > 180)
{
targetAngle -= 180;
}
targetAngle = -targetAngle;
targetPlaced = true;
rotated = false;
}
}
if (targetPlaced == true && rotated == false)
{
if (dbObjectAngleZ(name) < targetAngle)
{
dbRotateObject(name, dbObjectAngleX(name), dbObjectAngleY(name), dbObjectAngleZ(name) + 0.5);
}
if (dbObjectAngleZ(name) > targetAngle)
{
dbRotateObject(name, dbObjectAngleX(name), dbObjectAngleY(name), dbObjectAngleZ(name) - 0.5);
}
if (dbObjectAngleZ(name) >= targetAngle -0.5 && dbObjectAngleZ(name) <= targetAngle +0.5)
{
rotated = true;
}
}
if (targetPlaced == true && rotated == true)
{
dbMoveObject(name, 3);
}
if (dbObjectPositionY(name) >= yHit -50 && dbObjectPositionY(name) <= yHit +50
&& dbObjectPositionX(name) >= xHit -50 && dbObjectPositionX(name) <= xHit +50)
{
targetPlaced = false;
}
}
void shoot()
{
}
void die()
{
}
};
void DarkGDK ( void )
{
Tank tank0;
tank0.name = 1;
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbAutoCamOff();
dbMaximizeWindow();
dbLoadObject("map.x", 10000);
int map = 10000;
dbPositionObject(map, 0, 600, 300);
dbScaleObject(map, 1000, 1000, 1000);
dbRotateObject(map, 90, 0, 0);
//Important piece of code
***************************************************
* dbLoadObject("tank2.x", 1);
* dbScaleObject(1, 1000, 1000, 1000);
* dbRotateObject(1, -90, 0, 0);
***************************************************
float cameraX = 0;
float cameraY = 0;
float cameraZ = -500;
dbPositionCamera(cameraX, cameraY, cameraZ);
int i = 0;
// our main loop
while ( LoopGDK ( ) )
{
/*if (i == 0)
{
if (dbControlKey())
{
dbLoadObject("tank2.x", 1);
dbScaleObject(1, 1000, 1000, 1000);
dbRotateObject(1, -90, 0, 0);
i++;
}
}*/
tank0.move();
if (dbUpKey())
{
cameraY += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbDownKey())
{
cameraY -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbLeftKey())
{
cameraX -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbRightKey())
{
cameraX += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbSpaceKey())
{
cameraZ += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbShiftKey())
{
cameraZ -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
// update the screen
dbSync ( );
}
// return back to windows
return;
}
This is the code if i place the object loading inside the while loop, the object will only move when i hold the right mouse button in.
#include "DarkGDK.h"
#include <string>
using namespace std;
// the main entry point for the application is this function
int n = 1;
class Tank
{
public:
float name;
float speed;
float rotateSpeed;
float damage;
float health;
float xHit;
float yHit;
float deltaX;
float deltaY;
float targetAngle;
bool targetPlaced;
float rotate;
bool rotated;
float targetName;
float map;
Tank()
{
name = n;
map = 10000;
speed = 3;
rotateSpeed = 0.5;
damage = 20;
health = 100;
targetPlaced = false;
rotated = false;
}
void load()
{
dbLoadObject("tank2.x", n);
dbRotateObject(n, -90, 0, 0);
dbScaleObject(n, 1000, 1000, 1000);
n++;
}
void move()
{
if (dbPickObject(dbMouseX(), dbMouseY(), name, name) == name)
{
dbText(dbMouseX(), dbMouseY(), "Targeted");
if (dbMouseClick() == 1)
{
targetName = name;
}
}
if (targetName == name)
{
if (dbMouseClick() == 2)
{
dbPickObject( dbMouseX (), dbMouseY (), map, map );
xHit = dbGetPickVectorX() + dbCameraPositionX();
yHit = dbGetPickVectorY() + dbCameraPositionY();
deltaX = xHit - dbObjectPositionX(name);
deltaY = yHit - dbObjectPositionY(name);
targetAngle = dbATANFULL(deltaX, deltaY);
if (targetAngle > 180)
{
targetAngle -= 180;
}
targetAngle = -targetAngle;
targetPlaced = true;
rotated = false;
}
}
if (targetPlaced == true && rotated == false)
{
if (dbObjectAngleZ(name) < targetAngle)
{
dbRotateObject(name, dbObjectAngleX(name), dbObjectAngleY(name), dbObjectAngleZ(name) + 0.5);
}
if (dbObjectAngleZ(name) > targetAngle)
{
dbRotateObject(name, dbObjectAngleX(name), dbObjectAngleY(name), dbObjectAngleZ(name) - 0.5);
}
if (dbObjectAngleZ(name) >= targetAngle -0.5 && dbObjectAngleZ(name) <= targetAngle +0.5)
{
rotated = true;
}
}
if (targetPlaced == true && rotated == true)
{
dbMoveObject(name, 3);
}
if (dbObjectPositionY(name) >= yHit -50 && dbObjectPositionY(name) <= yHit +50
&& dbObjectPositionX(name) >= xHit -50 && dbObjectPositionX(name) <= xHit +50)
{
targetPlaced = false;
}
}
void shoot()
{
}
void die()
{
}
};
void DarkGDK ( void )
{
Tank tank0;
tank0.name = 1;
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbAutoCamOff();
dbMaximizeWindow();
dbLoadObject("map.x", 10000);
int map = 10000;
dbPositionObject(map, 0, 600, 300);
dbScaleObject(map, 1000, 1000, 1000);
dbRotateObject(map, 90, 0, 0);
//dbLoadObject("tank2.x", 1);
//dbScaleObject(1, 1000, 1000, 1000);
//dbRotateObject(1, -90, 0, 0);
float cameraX = 0;
float cameraY = 0;
float cameraZ = -500;
dbPositionCamera(cameraX, cameraY, cameraZ);
int i = 0;
// our main loop
while ( LoopGDK ( ) )
{
//Important piece of code
*********************************************************************
* if (i == 0)
* {
* if (dbControlKey())
* {
* dbLoadObject("tank2.x", 1);
* dbScaleObject(1, 1000, 1000, 1000);
* dbRotateObject(1, -90, 0, 0);
* i++;
* }
* }
*********************************************************************
tank0.move();
if (dbUpKey())
{
cameraY += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbDownKey())
{
cameraY -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbLeftKey())
{
cameraX -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbRightKey())
{
cameraX += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbSpaceKey())
{
cameraZ += 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
if (dbShiftKey())
{
cameraZ -= 5;
dbPositionCamera(cameraX, cameraY, cameraZ);
}
// update the screen
dbSync ( );
}
// return back to windows
return;
}