Move multiple CCSprite across the screen Cocos2dx - c++

I have an Init method and spawn() method which is called by a CCAction every 2 seconds!
What i want is to move pipePair node across the screen every 2 seconds, something like in the flappy bird game! But in my case am not able to add multiple pairs of the node in the screen plus the speed of the MoveBy CCAction fastens periodically.
So i want the nodes should be added periodically and they should move in a constant speed across the screen.
Any help would be appreciated.
bool HelloWorld::init()
{
//creating the world
b2Vec2 gravity;
gravity.Set(0.0f, -20.0f);
world = new b2World(gravity);
// Do we want to let bodies sleep?
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
setAccelerometerEnabled( true );
scheduleUpdate();
setTouchEnabled(true);
//////////////////////////////
// 1. super init first
if( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) //RGBA
{
return false;
}
if ( !CCLayer::init() )
{
return false;
}
screenSize = CCDirector::sharedDirector()->getWinSize();
//Initializing CCNodes
_moving = CCNode::create();
addChild(_moving);
_pipes = CCNode::create();
_moving->addChild(_pipes);
CCSprite *test = CCSprite::create("goalssss.png");
CCRect rectOfPipe = test->boundingBox();
float64 distanceToMove = screenSize.width + 2.5*rectOfPipe.size.width;
CCMoveBy* movePipes = CCMoveBy::create(0.01*distanceToMove, CCPointMake(-screenSize.width, 0));
CCRemoveSelf *removePipes = CCRemoveSelf::create();
sequenceActionOfPipes = CCSequence::create(movePipes,removePipes);
sequenceActionOfPipes->retain();
CCCallFunc *spawn = CCCallFunc::create(this, callfunc_selector(HelloWorld::spawnPipes));
CCDelayTime *delayForTwoSecs = CCDelayTime::create(2.0);
CCSequence *sequenceForSpawnAndDelay = CCSequence::create(spawn,delayForTwoSecs);
CCRepeatForever *repeatActionForeverOfSpawnAndDelay = CCRepeatForever::create(sequenceForSpawnAndDelay);
this->runAction(repeatActionForeverOfSpawnAndDelay);`
}
This is my spawn method which is called every 2 seconds!
void HelloWorld::spawnPipes(){
CCNode *pipePair = CCNode::create();
pipePair->setPosition(CCPoint(screenSize.width, 0));
//pipePair->setZOrder(-10);
float64 randomVal = arc4random()%(int32)(screenSize.height/3);
CCSprite *_pipeUpReplica = CCSprite::create("goalssss.png");
_pipeUpReplica->setPosition(CCPoint(20, randomVal));
CCSprite *_pipeBelowReplica = CCSprite::create("goalssss.png");
_pipeBelowReplica->setPosition(CCPoint(20, randomVal+100+_pipeUpReplica->boundingBox().size.height));
pipePair->addChild(_pipeUpReplica);
pipePair->addChild(_pipeBelowReplica);
//run actions
pipePair->runAction(sequenceActionOfPipes);
_pipes->addChild(pipePair);
}

Firs of all, you don't need to do this :
if( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) //RGBA
{
return false;
}
if ( !CCLayer::init() )
{
return false;
}
CCLayerColor inherits from CCLayer (reference : docs) and I certainly hope you don;t inherit from both of them! :)
As to the problem at hand, I can't pinpoit a problem, but I can spot some possible pitfalls, which may be contrbuting to your issues.
A lot of nodes. You have your Layer, on which you add the _moving node on which you add the _pipes node on which you add the pipePair node. Unless they are essential for other things in your game, I can;t see a reason why you are not adding the pipePair directly on your layer.
You didn't set the position of _moving and _pipes - though it may seem unimportant, it is good to explicilty position all your nodes to not be suprised later on. A usefull thing here is the `CCPointZero.
I have never had any success whatsoever with reusing CCSequences like you do. It always turned out to be best to just create them when they are to be used and not worry about them later on.
Hope some of these will help!

Try increasing the delay from 2.0f to 3.0f
CCDelayTime *delayForTwoSecs = CCDelayTime::create(3.0);

Related

Cocos 2dx game increasing in memory every time a scene transition occurs

I am making a cocos 2dx game. But each time the memory increases with every level transition. For debugging purposes I am calling the same scene again and again on touch event. Each level is generated by changing the parameters for the folowing code. Initially I thought that the memory was increasing because there were more objects in the higher levels, but even when calling the same level, memory occupied is increasing.
#include "GameScene.h"
#include "MainMenuScene.h"
#include "GameOverScene.h"
#include "Levels.h"
#define COCOS2D_DEBUG 1
USING_NS_CC;
float theta=0;
int r=0;
int levelNo=0;
int controlable=0; // flag to check if user can controll the ball or not
int rMax=0; // max radius of circle
float objectTime; // stores the inverse of speed
int secondCount=0; // second han value in the timer
int minuteCount=0; //minute hand clock in the timer
float obstacleSpeed=0;
Label *timer;
GameScene::~GameScene()
{
rotationPoint->removeAllChildrenWithCleanup(true);
obstacleRotationPoint->removeAllChildrenWithCleanup(true);
this->removeAllChildrenWithCleanup(true);
}
Scene* GameScene::createScene(int level)
{
// 'scene' is an autorelease object
auto scene = Scene::create();
controlable=0;
r=0;
theta=0;
// 'layer' is an autorelease object
levelNo=level;
rMax=levels[levelNo].ringCount * 15; //setting various parameters
obstacleSpeed =levels[levelNo].obstacleSpeed;
objectTime=1.0/levels[levelNo].speed;
secondCount=0; minuteCount=0;
auto layer = GameScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
controlable=0;
distance=rMax;
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
#if COMPILE_FOR_MOBILE == 1
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
goal = DrawNode::create();
goal->drawDot(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y), 5, Color4F(100,0,0,1));
this->addChild(goal,1); // drawing the goal
rotationPoint = Node::create();
rotationPoint->setPosition(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y);
this->addChild(rotationPoint, 2);
//Setting the exit button
auto exitLabel = Label::createWithTTF("Exit","fonts/Marker Felt.ttf",10);
exitButtonWidth=exitLabel->getContentSize().width;
exitButtonHeight=exitLabel->getContentSize().height;
exitLabel->setPosition(Point(visibleSize.width-exitButtonWidth,visibleSize.height-exitButtonHeight));
this->addChild(exitLabel);
//setting the clock
timer = Label::createWithTTF("00:00","fonts/Marker Felt.ttf",10);
timer->setPosition(Point(timer->getContentSize().width,visibleSize.height-timer->getContentSize().height));
this->schedule(schedule_selector(GameScene::updateClock),1.0f); //scedule to call upDateClock function every 1.0 sec
this->addChild(timer);
obstacleRotationPoint = Node::create();
obstacleRotationPoint->setPosition(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y);
this->addChild(obstacleRotationPoint, 3);
float theta=0;
snake[0] = DrawNode::create();
snake[0]->drawDot(Vec2(0,0),3,Color4F(100,110,0,1));
theta+=2*M_PI/150;
//this->addChild(snake[0],2);
rotationPoint->addChild(snake[0]);
// fixedPoint->addChild(snake[0]);
//loop to draw the concentric circles
for(r=15;r<=rMax;r+=15)
{
for(theta=0;theta<=2*M_PI;theta+=2*M_PI/r){
pathNode = DrawNode::create();
pathNode->drawDot(Vec2(r*cos(theta)+origin.x+visibleSize.width/2,r*sin(theta)+origin.y+visibleSize.height/2),1,Color4F(0,0,10,1));
//pathNode->autorelease();
this->addChild(pathNode,1);
//this->removeChild(pathNode);
}
}
controlable=0;
this->scheduleUpdate();
return true;
}
bool GameScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{ // check if exit button region was clicked
_eventDispatcher->removeAllEventListeners();
auto scene = GameScene::createScene(levelNo);
Director::getInstance()->replaceScene(scene);
return true;
}
//function updates every frame
void GameScene::update(float dt){
}
The last part in the init function where I need to add pathNode is increasing my memory requirement everytime I transition a scene. I believe I am releasing everything in my destructor.
First of all, I don't recommend using global variables, which you have on top of your file (especially timer label). You should keep everything in class.
Second, you should check whether destructor is called in the first place.
Third, you can also try using some "Loading" screen between two levels and clean all unused textures like this:
setOnExitCallback([&](){
Director::getInstance()->getTextureCache()->removeUnusedTextures();
});
Fourth, I'd recommend to not recreate GameScene at all, but create function like restartLevel() and loadLevel() and just remove unnecessary stuff there and load new one.

Sprite not drawing/appearing on screen, Cocos2d

I'v got trouble getting my sprite to appear on screen using my current method. I got multiple sprites on the screen already, but I'v added this new one in a different manner as I'm going to manipulate it later on (handling different interactions and such). But I don't see why it doesn't appear on the screen when I run the program. I'v tried changing the z order without luck, but that might be because the sprite wasn't added in the init method like the others so the z order doesn't affect it in comparison to the background and such created in the init method. Anyways, got any clue for why it won't draw on screen?
- (void) addMonster {
CCSprite * monster = [CCSprite spriteWithImageNamed:#"Ghost.png"];
// Determine where to spawn the monster along the Y axis
CGSize viewSize = [CCDirector sharedDirector].viewSize;
int minY = monster.contentSize.height / 2;
int maxY = viewSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(viewSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];
// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
CCActionMoveTo * actionMove = [CCActionMoveTo actionWithDuration:actualDuration
position:ccp(-monster.contentSize.width/2, actualY)];
CCActionCallBlock * actionMoveDone = [CCActionCallBlock actionWithBlock:^(CCNode *node) {
[monster removeFromParentAndCleanup:YES];
}];
[monster runAction:[CCActionSequence actions:actionMove, actionMoveDone, nil]];
}
//The call back function
-(void)gameLogic:(CCTime)dt {
[self addMonster];
}
- (id)init{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
if( (self = [super init]) ) {
self.userInteractionEnabled = YES;
//How often a new ghost gets produced
[self schedule:#selector(gameLogic:) interval:1.0];
//And some more code none relevant to this keeps going...
I am having the same problem.
The sprites i add do appear, but when i leave the scene and come back, they won't appear. As far as i know sprites needs to be added in the init method. When you add them at a later stage (in my case) they appear only once.
I am still looking for a solution to add the sprites at a later stage than the init method and appears more than once.
Maybe this answer helps you finding a solution.

Stuck trying to implement touch and hold (bullet firing)? - cocos2d-x

I've been having a lot of trouble implementing a smooth and reliable touch-and-hold solution for firing bullet (sprites) - even after looking at other people's solutions.
The solution has to switch between touch-began, touch moved and touch ended seamlessly: always firing bullets at the touch location until the finger is released. At the moment I have many problems with reliability and stability with every case but touchmoved, which works fine.
The exact issue is around half the time a finger is held down (touchBegan + scheduler) the bullets appear but disappear a second later but other times they move towards the touch perfectly - something is deleting them and i don't have much experience with schedulers or actions to know what.
Heres my code, I've used 2 different firing methods: one scheduled to run every 0.05 seconds after touchBegan, and one triggered every time touchMoved is detected. The touchMoved one works fine, but getting it to work with the unreliable touchBegan one is trouble. The really annoying part is even if I remove the touch part and just schedule sprites to appear and run scheduled actions non-stop from init, the same reliability problem happens (disappearing/deletion). Maybe I dont understand getting schedulers and actions to play nice, or maybe theres a better touch and hold method? Thanks in advance for any help.
bool HelloWorld::init()
{
... miscellaneous sprite creation
this->schedule(schedule_selector(HelloWorld::fireBullets), 0.05);
}
void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch* touch = (CCTouch*)( pTouches->anyObject() ); // get single-touch as opposed to multitouch
touchLocation = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
if (touchLocation.x > 400)
{
float dX = touchLocation.x - gun->getPosition().x;
float dY = touchLocation.y - gun->getPosition().y;
touchAngle = atan2(dY, dX);
gun->setRotation(-CC_RADIANS_TO_DEGREES(touchAngle));
cursor->setPosition(touchLocation);
screenHeld = true;
}
}
void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch* touch = (CCTouch*)( pTouches->anyObject() ); // for single-touch as opposed to multitouch
touchLocation = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
if (touchLocation.x > 400)
{
float dX = touchLocation.x - gun->getPosition().x;
float dY = touchLocation.y - gun->getPosition().y;
float angle = atan2(dY, dX);
gun->setRotation(-CC_RADIANS_TO_DEGREES(angle));
cursor->setPosition(touchLocation);
screenHeld = false; //not technically true but touchMoved bullet firing works differently (not scheduled, every movement instead)
if (getTimeTick() - lastBulletFire > 50) //getTickTime is simple get system time method, works fine
{
fireBullet(angle);
}
}
}
//this is for touchBegan and has issues, scheduled to run every 50ms touch-held
void HelloWorld::fireBullets(CCTime dt)
{
if (screenHeld)
{
CCSprite* bullet = CCSprite::create("bullet.png");
bullet->setPosition(ccp(gun->getPosition().x, gun->getPosition().y));
bullet->setRotation(-CC_RADIANS_TO_DEGREES(touchAngle));
//send bullet towards touchlocation
bullet->runAction(CCSequence::create(CCMoveBy::create(1.5f, ccp(800 * cos(touchAngle), 800 * sin(touchAngle))), NULL));
this->addChild(bullet, 5);
}
}
//this is for touchMoved and works fine, everytime finger is moved bullet fired
void HelloWorld::fireBullet(float angle)
{
CCSprite* bullet = CCSprite::create("bullet.png");
bullet->setPosition(ccp(gun->getPosition().x, gun->getPosition().y)); //add a random spread to the y value (or maybe the y-value of the destination)
this->addChild(bullet, 5);
bullet->setRotation(-CC_RADIANS_TO_DEGREES(angle));
bullet->runAction(CCSequence::create(CCMoveBy::create(1.5f, ccp(800 * cos(angle), 800 * sin(angle))), NULL));
lastBulletFire = getTimeTick();
}
Found the solution, it wasn't anything to do with actions/schedulers but the fact that I wasn't calling retain() on the bullets. Normally I'd manage C++ memory manually but this hybrid style threw my off

How to duplicate sprite of sprites in cocos2d-x 2.1.4

Well, my question is simple, I'm starting with cocos2d-x programming and I'm trying to make a tiled infinite background, lets say I have a png of an image I want to repeat infinitely as a background, What I did was that I created a Sprite as a container, then I added sprites with the same image, one aside the next, covering about 110% the screen, then, following the Space game tutorial I created the same sprite of sprites once again as a second background and I use it as two large identical images to scroll infinite... it works perfect... but I'm wondering if it is possible to create the second sprite by just copying the frist. Some code maybe will clear my situation:
this goes in the init():
acum = 0.0;
city1 = CCSprite::create();
for(int i = 0; i < 12; ++i) {
CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
city->setPosition(ccp(acum, 0));
city1->addChild(city);
acum+= city->getContentSize().width*0.99;
}
city1->setContentSize(ccp(acum, 0));
city2 = CCSprite::create();
acum = 0.0;
for(int i = 0; i < 12; ++i) {
CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
city->setPosition(ccp(acum, 0));
city2->addChild(city);
acum+= city->getContentSize().width*0.99;
}
city2->setContentSize(ccp(acum, 0));
_backgroundNode->addChild(city1, -1 , buildingspeed, ccp(0, winSize.height * 0.6));
_backgroundNode->addChild(city2, -1 , buildingspeed, ccp(city1->getContentSize().width, winSize.height * 0.6));
printf("%f - %f\n", city1->getContentSize().width, city2->getContentSize().width);
The main problem is here, where I need to create city2 from city1 and not just repeat code... is there a way to do this? I don't see a constructor in CCSprite that allows me to do so...
this goes in the update():
CCArray *cities = CCArray::createWithCapacity(2);
cities->addObject(city1);
cities->addObject(city2);
for ( int ii = 0; ii <cities->count(); ii++ ) {
CCSprite * city = (CCSprite *)(cities->objectAtIndex(ii));
float xPosition = _backgroundNode->convertToWorldSpace(city->getPosition()).x;
float size = city->getContentSize().width;
if ( xPosition < -size ) {
_backgroundNode->incrementOffset(ccp(city->getContentSize().width*2,0),city);
}
}
I will appreciate any help, thanks in advance.
This code will create the exact snapshot of your citySprite. And as this is a snapshot, you'll be having no access to the sprites that it contains. Cause they are now embedded in the image or sprite itself.
float citySpriteWidth = 480;
float citySpriteHeight = 320;
//Set position in order to make it fit inside CCRenderTexture (You can change this later)
citySprite->setPosition(ccp(citySpriteWidth/2, citySpriteHeight/2));
CCRenderTexture *render = CCRenderTexture::renderTextureWithWidthAndHeight(citySpriteWidth, citySpriteWidth);
render->beginWithClear(0, 0, 0, 0);
citySprite->visit();
render->end();
CCTexture2D *tex = render->getSprite()->getTexture();
CCSprite *newCitySprite = CCSprite::spriteWithTexture(tex);
newCitySprite->setFlipY(true); //Texture might be upside down
Hope this helps your requirements.
Else if your problem is just code repetition. Then you can write a function which would give you your city sprite containing other sprites like this one. And call it number of times you want it.
CCSprite* creteCitySprite()
{
float acum = 0.0;
CCSprite *city1 = CCSprite::create();
for(int i = 0; i < 12; ++i)
{
CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
city->setPosition(ccp(acum, 0));
city1->addChild(city);
acum+= city->getContentSize().width*0.99;
}
city1->setContentSize(CCSizeMake(acum, 0));
return city1;
}
EDITED:
This gives you what you want. But what I wanted to say is, the sub sprites which you added to your city1 will remain static.
CCSptire *getSpriteFromSprite(CCSprite *citySprite, float citySpriteWidth, float citySpriteHeight)
{
CCPoint prevPosition = citySprite->getPosition();
//Set position in order to make it fit inside CCRenderTexture (You can change this later)
citySprite->setPosition(ccp(citySpriteWidth/2, citySpriteHeight/2));
CCRenderTexture *render = CCRenderTexture::renderTextureWithWidthAndHeight(citySpriteWidth, citySpriteWidth);
render->beginWithClear(0, 0, 0, 0);
citySprite->visit();
render->end();
citySprite->setPosition(prevPosition);
CCTexture2D *tex = render->getSprite()->getTexture();
CCSprite *newCitySprite = CCSprite::spriteWithTexture(tex);
newCitySprite->setFlipY(true); //Texture might be upside down
}

Is object remain fixed when scrolling background in cocos2d

I have one question when infinite background scrolling is done, is the object remain fixed(like doodle in doodle jump, papy in papi jump) or these object really moves.Is only background move or both (background and object )move.plz someone help me.I am searching for this solution for 4/5 days,but can't get the solution.So plz someone help me. And if object does not move how to create such a illusion of object moving.
If you add the object to the same layer as the scrolling background, then it will scroll as the background scrolls.
If your looking for an effect like the hero in doodle jump, you may want to look at having two or more layers in a scene.
Layer 1: Scrolling Background Layer
Layer 2: Sprite layer
SomeScene.m
CCLayer *backgroundLayer = [[CCLayer alloc] init];
CCLayer *spriteLayer= [[CCLayer alloc] init];
[self addChild:backgroundLayer z:0];
[self addChild:spriteLayer z:1];
//Hero stays in one spot regardless of background scrolling.
CCSprite *squidHero = [[CCSprite alloc] initWithFile:#"squid.png"];
[spriteLayer addChild:squidHero];
If you want objects to scroll with the background add it to the background layer:
//Platform moves with background.
CCSprite *bouncePlatform= [[CCSprite alloc] initWithFile:#"bouncePlatform.png"];
[backgroundLayer addChild:bouncePlatform];
Another alternative is to use a CCFollow action. You would code as if the background is static (which it will be) and the player is moving (which it will be), but add a CCFollow action to the player. This essentially moves the camera so that it tracks your player.
You can also modify the classes so that you can get the CCFollow action to follow with an offset (i.e., so the player is not in the middle of the screen) as well as to have a smoothing effect to it, so that when the player moves, the follow action is not jerky. See the below code:
*NOTE I am using cocos2d-x, the c++ port. The methods are similar in cocos2d, and you should be able to modify these to fit the cocos2d syntax. Or search around -- I found these for cocos2d and then ported to c++.
//defines the action to constantly follow the player (in my case, "runner.p_sprite is the sprite pointing to the player)
FollowWithOffset* followAction = FollowWithOffset::create(runner.p_sprite, CCRectZero);
runAction(followAction);
And separately, I have copied the class definition for CCFollow to create my own class, CCFollowWithAction. This also has a smoothing effect (you can look this up more online) so that when the player moves, the actions are not jerky. I modified "initWithTarget," to take into account an offset, and "step," to add a smoothing action. You can see the modifications in the comments below.
bool FollowWithOffset::initWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)
{
CCAssert(pFollowedNode != NULL, "");
pFollowedNode->retain();
m_pobFollowedNode = pFollowedNode;
if (rect.equals(CCRectZero))
{
m_bBoundarySet = false;
}
else
{
m_bBoundarySet = true;
}
m_bBoundaryFullyCovered = false;
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
m_obFullScreenSize = CCPointMake(winSize.width, winSize.height);
//m_obHalfScreenSize = ccpMult(m_obFullScreenSize, 0.5f);
m_obHalfScreenSize = CCPointMake(m_obFullScreenSize.x/2 + RUNNER_FOLLOW_OFFSET_X,
m_obFullScreenSize.y/2 + RUNNER_FOLLOW_OFFSET_Y);
if (m_bBoundarySet)
{
m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x);
m_fRightBoundary = -rect.origin.x ;
m_fTopBoundary = -rect.origin.y;
m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y);
if(m_fRightBoundary < m_fLeftBoundary)
{
// screen width is larger than world's boundary width
//set both in the middle of the world
m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary) / 2;
}
if(m_fTopBoundary < m_fBottomBoundary)
{
// screen width is larger than world's boundary width
//set both in the middle of the world
m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary) / 2;
}
if( (m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary) )
{
m_bBoundaryFullyCovered = true;
}
}
return true;
}
void FollowWithOffset::step(float dt)
{
CC_UNUSED_PARAM(dt);
if(m_bBoundarySet){
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
if(m_bBoundaryFullyCovered)
return;
CCPoint tempPos = ccpSub( m_obHalfScreenSize, m_pobFollowedNode->getPosition());
m_pTarget->setPosition(ccp(clampf(tempPos.x, m_fLeftBoundary, m_fRightBoundary),
clampf(tempPos.y, m_fBottomBoundary, m_fTopBoundary)));
}
else{
//custom written code to add in support for a smooth ccfollow action
CCPoint tempPos = ccpSub( m_obHalfScreenSize, m_pobFollowedNode->getPosition());
CCPoint moveVect = ccpMult(ccpSub(tempPos,m_pTarget->getPosition()),0.25); //0.25 is the smooth constant.
CCPoint newPos = ccpAdd(m_pTarget->getPosition(), moveVect);
m_pTarget->setPosition(newPos);
}
}