დამატებულია
1. GameObject.cpp და GameObject.h თამაშის ობიექტების კლასი და
2. Player.cpp და Player.h მოთამაშის კლასი, რომელიც არის GameObject კლასის ქვეკლასი
ამიტომაც მისი ორი ცვლადი
protected:
uint _Type;
Vec2 _Pos;
არის protected და არა private, იმიტომ რომ დაინახოს ქვეკლასმა.
ამ ორივე კლასში (GameObject და Player) არის მეთოდები რომლებიც წერს ცვლადებში მნიშვნელობებს და მოაქვს ეს მნიშვნელობები.
GameConstants.h ფაილში დაემატა ერთი მუდმივა #define Z_ORDER_PLAYER 110
GameScene კლასს დაემატა ორი ცვლადი:
Player *_MainPlayer; // მთავარი მოთამაშე
Vec2 _PlayerVelocity; // მოთამაშის სიჩქარე გამოთვლილი გიროსკოპის მიერ
ასევე დაემატა 4 მეთოდი:
void CreateObjects(); // ყველა ობიექტის შექმნის მეთოდი
virtual void onAcceleration(Acceleration *acc, Event *unused_event); // გიროსკოპის მოძრაობა
void updatePosition(); // მდებარეობის განახლება
void update(float deltaT); // მეთოდი რომელიც გამოიძახება ყოველ 0.0167 წამში
CreateObjects: მთავარი მოთამაშის შექმნა, ქვედა მარცხენა კუთხეში.
void GameScene::CreateObjects()
{
_MainPlayer = new Player(1, Vec2(_VisibleSize.width * 0.12f, _VisibleSize.height * 0.12f));
this->addChild(_MainPlayer->_Spr, Z_ORDER_PLAYER);
}
onAcceleration: გიროსკოპით დადგენა თუ საით უნდა იმოძრაოს მთავარმა პერსონაჟმა.
void GameScene::onAcceleration(Acceleration *acc, Event *unused_event) // Acceleration (Device rotation)
{
if(_GameIsPaused == true ) return;float acceleration = 2.0f; // acceleration
float maxVelocity = 2; // Max speed_PlayerVelocity.x = acc->x * acceleration;
_PlayerVelocity.y = acc->y * acceleration;if (_PlayerVelocity.x > maxVelocity)
{
_PlayerVelocity.x = maxVelocity;
}
else if (_PlayerVelocity.x < -maxVelocity)
{
_PlayerVelocity.x = -maxVelocity;
}if (_PlayerVelocity.y > maxVelocity)
{
_PlayerVelocity.y = maxVelocity;
}
else if (_PlayerVelocity.y < -maxVelocity)
{
_PlayerVelocity.y = -maxVelocity;
}
}
updatePosition: მთავარი პერსონაჟის მდებარეობის ცვლილება, მანამდე შემოწმებები რომ არ გაცდეს ეკრანის ჩარჩოებს
void GameScene::updatePosition() // Update player position
{Vec2 pos = _MainPlayer->GetObjectPos();
pos.x += _PlayerVelocity.x;
pos.y += _PlayerVelocity.y;float imageWidthHalved = 48.0f;
float imageHeightHalved = 48.0f;float leftBorderLimit = imageWidthHalved;
float rightBorderLimit = _VisibleSize.width – imageWidthHalved;float topBorderLimit = imageHeightHalved;
float bottomBorderLimit = _VisibleSize.height – imageHeightHalved;// If out of border, stop player
if (pos.x < leftBorderLimit)
{
pos.x = leftBorderLimit;
_PlayerVelocity.x = 0;
}
else if (pos.x > rightBorderLimit)
{
pos.x = rightBorderLimit;
_PlayerVelocity.x = 0;
}if (pos.y < topBorderLimit)
{
pos.y = topBorderLimit;
_PlayerVelocity.y = 0;
}
else if (pos.y > bottomBorderLimit)
{
pos.y = bottomBorderLimit;
_PlayerVelocity.y = 0;
}// CCLOG(“x = %f y = %f”, _PlayerVelocity.x, _PlayerVelocity.y);
_MainPlayer->SetObjectDir(_PlayerVelocity);
_MainPlayer->UpdatePosition();
}
update: თუ თამაში შეჩერებული არ არის გამოვიძახომ მთავარი პერსონაჟის მდებარეობის ცვლილების მეთოდი
void GameScene::update(float deltaT) // Method called every 0.0167 second
{
if (_GameIsPaused == false)
{
updatePosition();
}
}