Hello, I am beginner in game programming. I have the foll code:
`HelloWorldScene.cpp
void HelloWorld::ccTouchesBegan(CCSet* touches, CCEvent* event)
{
CCTouch* touch = (CCTouch*)(touches->anyObject());
CCPoint location = touch->locationInView(touch->view());
location = CCDirector::sharedDirector()->convertToGL(location);
Player p1;
p1.PlayerTouch(location);
}
The problem is even after the if statement is true, I cannot run action on the sprite which is touched. I have defined the pSprite and the action in the init() function. Basically I cannot perform any action like creating a new sprite or label or changing it, if I access the function from the parent class.
The compiler dosen’t give any error and rest of the code is working fine. Am I making a mistake in the concept?? How do I define the object for the Player class??? Pleas help me … Thanks in Advance
I have added the sprite in the init() function of Player class. And it is drawing perfectly on the screen. Sorry I did not give the full code here it is …
@ #include “Player.h” #include “HelloWorldScene.h”
USING_NS_CC;
// on “init” you need to initialize your instance
bool Player::init()
{
//////////////////////////////
// 1. super init first
bool bRet=false;
do{
CC_BREAK_IF(! CCLayer::init());
//HelloWorld::init();
this~~>setIsTouchEnabled;
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
pSprite = CCSprite::spriteWithFile;
pSprite~~>setPosition );
this~~>addChild(pSprite, 2);
My problem is I have called the PlayerTouch function by creating an object in HelloWorldScene, and it is calling the function perfectly as I can see the “Rect” log, but the pSprite runAction is not performing any action which I defined. So, how can I run the action by calling it from the main function?
Basically my question is how can I create different objects for the Player class, and then use the function. The way I have defined the object, it seems to work fine … Just that it does not add a sprite or as in the code above does not run the action which I have defined … Pls do check out and help me ….
I am sorry its getting a bit complicated for me…
What I simply want to do is create a seperate class for every game entity … like a seperate class for player and the bullet he fires and all the functions defined inside that class… and then effectively call these classes from the main scene … I want to make the same game as in the tutorial you have provided on the site … But with seperate classes for player, enemy and bullet … Do you have any link where I can understand this? or any source code where they have implemented like this?? It would be a real help if I understand how to do it …