Rats! I just added a big post and it disappeared when I submitted it!
Try this code. I replaced the two calls to GetSpriteX when you move to just using the last x you retrieved. There will have been absolutely no movement since you called GetSpriteX at the top of the loop.
#include "template.h"
#include <iostream>
using namespace AGK;
app App;
#define playerID 1
#define PLAYERSTARTHEIGHT 0.f
// App prototype
void AppForceExit ( void );
// outside any functions, global definitions
float midScreen, playerWid, playerOffset;
// Begin app, called once at the start
void app::Begin( void )
{
// use device resolution
agk::SetVirtualResolution ( m_DeviceWidth, m_DeviceHeight );
agk::CreateSprite(agk::LoadImage("/Users/maxmarze/Documents/AGK_BETA/AGK/IDE/templates/template_mac_xcode4 2/background1.jpg"));
agk::CreateSprite(playerID, agk::LoadImage("/Users/maxmarze/Documents/AGK_BETA/AGK/IDE/templates/template_mac_xcode4 2/ship_one.png"));
agk::SetSpritePosition(playerID, 16.f, PLAYERSTARTHEIGHT);
agk::SetSpriteScale(playerID, 2.f, 2.f);
agk::SetSpritePhysicsOn(playerID);
agk::SetSpritePhysicsCanRotate(playerID, 0);
// set screen width and other calculations
midScreen = agk::GetVirtualWidth() / 2.0f;
playerWid = agk::GetSpriteWidth(playerID) / 2.0f;
playerOffset = midScreen + playerWid;
}
// Main loop, called every frame
void app::Loop ( void )
{
// say hello
agk::Print ( "Hello Mr Mac" );
float speed = 1000.f;
float x = agk::GetSpriteX(playerID);
float y = agk::GetSpriteY(playerID);
// update based on changes since last frame
float mathStuff = x - playerOffset;
agk::SetViewOffset(mathStuff, 0);
// check for movement keys
if(agk::GetRawKeyState(65) == 1)
{
agk::SetSpritePhysicsImpulse(playerID, x, y, x -speed, y);
std::cout << "Player X, pressing left: " << x << std::endl;
} else if(agk::GetRawKeyState(68) == 1) {
agk::SetSpritePhysicsImpulse(playerID, x, y, x + speed, y);
std::cout << "Player X, pressing right: " << x << std::endl;
} else if(agk::GetRawKeyState(87) == 1) {
agk::SetSpritePhysicsImpulse(playerID, x, y, x, y - 15000.f);
} else {
// nothing being pressed, stop player
agk::SetSpritePhysicsVelocity(playerID, 0, 0);
}
agk::Sync();
}
No matter how many things you do to a sprite, you won't see any changes until the Sync() is finished.
And you will speed up your overall performance by using global values for the the display and sprite widths used in your mathStuff calculations.
So, since you actually want to catch the changes made the previous frame, I moved your mathStuff calculation (using previously calculated values) and view offset to the top of the loop.
The combination of my changes removes up to four function calls, two divisions and one addition. Every little bit helps in making an app run smoother.
And, in Tier 2, user inputs (like key presses and tilts and such) are only checked once each app::Loop call, no matter how many Sync() calls you make.
So there is no point rechecking the position of a sprite (as you were in your outputs), that just takes up more processing time.
(I said this all much better the first time I typed it.)
Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master