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

126 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()
{
_ballStartingVelocity = ccp(20.0f, -100.0f);
_ball = Ball::ballWithTexture( CCTextureCache::sharedTextureCache()->addImage(s_Ball) );
_ball->setPosition( VisibleRect::center() );
_ball->setVelocity( _ballStartingVelocity );
addChild( _ball );
_ball->retain();
CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(s_Paddle);
CCArray *paddlesM = CCArray::createWithCapacity(4);
Paddle* paddle = Paddle::paddleWithTexture(paddleTexture);
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::bottom().y + 15) );
paddlesM->addObject( paddle );
paddle = Paddle::paddleWithTexture( paddleTexture );
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) );
paddlesM->addObject( paddle );
paddle = Paddle::paddleWithTexture( paddleTexture );
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::bottom().y + 100) );
paddlesM->addObject( paddle );
paddle = Paddle::paddleWithTexture( paddleTexture );
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) );
paddlesM->addObject( paddle );
_paddles = (CCArray*)paddlesM->copy();
CCObject* pObj = NULL;
CCARRAY_FOREACH(_paddles, pObj)
{
paddle = (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 = ccpMult(_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;
CCObject* pObj = NULL;
CCARRAY_FOREACH(_paddles, pObj)
{
paddle = (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()
{
2010-11-11 11:18:58 +08:00
CCDirector::sharedDirector()->replaceScene(this);
2010-08-27 11:53:35 +08:00
}