I'm trying to create a scroll method to move my background sprites. But when I refer to the background sprite in the method i get this message:
Use of undeclared identifier 'bg2'
This is how I'm trying to use bg2 in scroll method:
void scroll() {
Point pos2 = bg2.getPosition();
pos1.x -= 5.0f;
pos2.x -= 5.0f;
if(pos1.x <=-(visibleSize.width*0.5f) )
{
pos1.x = pos2.x + winSize.width;
}
if(pos2.x <=-(winSize.width*0.5f) )
{
pos2.x = pos1.x + winSize.width;
}
background1.setPosition(pos1);
background2.setPosition(pos2);
}
But I declared bg2 in helloWorld.h, like this:
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
//windows size
cocos2d::Size visibleSize;
cocos2d::Point origin;
//state machines
char gameState;
char playerState;
//sprites
cocos2d::Sprite* player;
cocos2d::Sprite* bg1;
cocos2d::Sprite* bg2;
cocos2d::Sprite* bg3;
cocos2d::Sprite* bg4;
cocos2d::Sprite* bg5;
cocos2d::Sprite* bg6;
cocos2d::Sprite* bg7;
cocos2d::Sprite* bg8;
cocos2d::Sprite* bg9;
cocos2d::Sprite* bg10;
cocos2d::Sprite* bg11;
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event * event);
void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event * event);
void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event * event);
void scroll();
};
I also defined bg2 in the init method of helloWorld.cpp
//define background
bg1 = Sprite::create("005_chao_base.png");
bg1->setAnchorPoint(Point(0,0));
bg1->setPosition(Point(0, 0));
this->addChild(bg1,-1);
bg2 = Sprite::create("005_chao_base.png");
bg2->setAnchorPoint(Point(0,0));
bg1->setPosition(Point(bg1->getBoundingBox().size.width, 0));
this->addChild(bg2,-1);
I'm declaring the scroll method like this in .cpp
void scroll() {
}
and in .h
void scroll();
cocos2d::Sprite
is a pointer notice the *
. So it would be bg2->getPosition()
also, you are doing cocos2d::Sprite* bg2;
but using: bg1 = Sprite::create("005_chao_base.png");
also, get in the habit of name spacing everything. You are inconsistent. Sometimes you use cocos2d::Sprite*
others you dont use cocos2d::
Edit: Also, you should namespace scroll like HelloWorld::Scroll
in .cpp