axmol/samples/Cpp/TestCpp/Classes/TouchesTest/TouchesTest.cpp

127 lines
3.2 KiB
C++
Raw Normal View History

#include "TouchesTest.h"
#include "Ball.h"
#include "Paddle.h"
#include "../testResource.h"
2010-08-27 11:53:35 +08:00
enum tagPlayer
{
kHighPlayer,
kLowPlayer
} PlayerTouches;
2010-08-27 11:53:35 +08:00
2012-05-30 16:55:46 +08:00
#define kStatusBarHeight 0.0f //20.0f
2010-08-27 11:53:35 +08:00
//#define k1UpperLimit (480.0f - kStatusBarHeight)
enum
{
kSpriteTag
2010-08-27 11:53:35 +08:00
};
//------------------------------------------------------------------
//
// PongScene
//
//------------------------------------------------------------------
PongScene::PongScene()
{
PongLayer *pongLayer = new PongLayer();//PongLayer::create();
addChild(pongLayer);
pongLayer->release();
2010-08-27 11:53:35 +08:00
}
//------------------------------------------------------------------
//
// PongLayer
//
//------------------------------------------------------------------
PongLayer::PongLayer()
{
2013-07-12 14:11:55 +08:00
_ballStartingVelocity = Point(20.0f, -100.0f);
_ball = Ball::ballWithTexture( TextureCache::getInstance()->addImage(s_Ball) );
_ball->setPosition( VisibleRect::center() );
_ball->setVelocity( _ballStartingVelocity );
addChild( _ball );
_ball->retain();
Texture2D* paddleTexture = TextureCache::getInstance()->addImage(s_Paddle);
Array *paddlesM = Array::createWithCapacity(4);
Paddle* paddle = Paddle::createWithTexture(paddleTexture);
2013-07-12 14:11:55 +08:00
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 15) );
paddlesM->addObject( paddle );
paddle = Paddle::createWithTexture( paddleTexture );
2013-07-12 14:11:55 +08:00
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) );
paddlesM->addObject( paddle );
paddle = Paddle::createWithTexture( paddleTexture );
2013-07-12 14:11:55 +08:00
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 100) );
paddlesM->addObject( paddle );
paddle = Paddle::createWithTexture( paddleTexture );
2013-07-12 14:11:55 +08:00
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) );
paddlesM->addObject( paddle );
_paddles = paddlesM->clone();
_paddles->retain();
Object* pObj = NULL;
CCARRAY_FOREACH(_paddles, pObj)
{
paddle = static_cast<Paddle*>(pObj);
if(!paddle)
break;
addChild(paddle);
}
schedule( schedule_selector(PongLayer::doStep) );
2010-08-27 11:53:35 +08:00
}
PongLayer::~PongLayer()
{
_ball->release();
_paddles->release();
2010-08-27 11:53:35 +08:00
}
void PongLayer::resetAndScoreBallForPlayer(int player)
{
_ballStartingVelocity = _ballStartingVelocity * -1.1f;
_ball->setVelocity( _ballStartingVelocity );
_ball->setPosition( VisibleRect::center() );
// TODO -- scoring
2010-08-27 11:53:35 +08:00
}
2012-06-08 13:55:28 +08:00
void PongLayer::doStep(float delta)
2010-08-27 11:53:35 +08:00
{
_ball->move(delta);
2010-08-27 11:53:35 +08:00
Paddle* paddle = NULL;
Object* pObj = NULL;
CCARRAY_FOREACH(_paddles, pObj)
{
paddle = static_cast<Paddle*>(pObj);
2010-08-27 11:53:35 +08:00
if(!paddle)
break;
2010-08-27 11:53:35 +08:00
_ball->collideWithPaddle( paddle );
}
2010-08-27 11:53:35 +08:00
if (_ball->getPosition().y > VisibleRect::top().y - kStatusBarHeight + _ball->radius())
resetAndScoreBallForPlayer( kLowPlayer );
else if (_ball->getPosition().y < VisibleRect::bottom().y-_ball->radius())
resetAndScoreBallForPlayer( kHighPlayer );
_ball->draw();
2010-08-27 11:53:35 +08:00
}
void PongScene::runThisTest()
{
Director::getInstance()->replaceScene(this);
2010-08-27 11:53:35 +08:00
}