Problems with accessing function in Child Class

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);
}

Player.cpp
void Player::PlayerTouch(CCPoint loc)
{
Player::init();
if (CCRect::CCRectContainsPoint(pSpriteRect1, loc))
{
CCLog(“Rect”);
pSprite->runAction(CCSequence::actions(scaleMon,NULL));
}
}`

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 :slight_smile:

The sprite is not added to a layer or a scene, it will not be drawn.

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);

pSpriteRect1 = CCRectMake(
pSprite~~>getPosition.x~~ (pSprite~~>getContentSize.width/2),
pSprite~~>getPosition().y - (pSprite~~>getContentSize.height/2),
pSprite~~>getContentSize().width,
pSprite~~>getContentSize.height);
scaleMon = CCScaleTo::actionWithDuration 1, 0.5);
bRet = true;
} while ;
return bRet;
}
void Player::PlayerTouch
{
Player::init;
if )
{
CCLog;
Player::pSprite~~>runAction(CCSequence::actions(scaleMon,NULL));
}
loc_x = loc.x;
}
@

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?

What’s Player?
Is it a layer?
I think you should upload the whole project.

yes, Player is a layer which I added to the scene … here’s the whole HelloworldScene.cpp code. …

#include “HelloWorldScene.h”
#include “Player.h”
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// ‘scene’ is an autorelease object
CCScene scene = NULL;
do
{
scene = CCScene::node;
CC_BREAK_IF;
HelloWorld
layer = HelloWorld::node();
CC_BREAK_IF(! layer);
scene~~>addChild;
Player layer1 = Player::node;
CC_BREAK_IF;
scene~~>addChild;
} while;
return scene;
}
bool HelloWorld::init
{
//////////////////////////////
// 1. super init first
if ) )
{
return false;
}
this~~>setIsTouchEnabled;
CCMenuItemImage
pCloseItem = CCMenuItemImage::itemFromNormalImage );
pCloseItem~~>setPosition( ccp(CCDirector::sharedDirector()>getWinSize.width 20, 20) );

// create menu, it’s an autorelease object
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu~~>setPosition;
this~~>addChild(pMenu, 1);
//this->schedule(schedule_selector(HelloWorld::gameLogic),4);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSize size = CCDirector::sharedDirector()>getWinSize;
CCSprite* pSprite = CCSprite::spriteWithFile;
pSprite
>setPosition( ccp(size.width/2, size.height/2) );
return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()>end;
#if
exit;
#endif
}
void HelloWorld::ccTouchesEnded
{
CCTouch* touch = );
CCPoint location = touch
>locationInView(touch->view());
location = CCDirector::sharedDirector()->convertToGL(location);
Player p1;
p1.Player::PlayerTouch(location);
}

void HelloWorld::registerWithTouchDispatcher()
{
// CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
}

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 ….

You create a new layer every you touch the scene?
What do you want to do?
The created layer in HelloWorld::ccTouchesEnded() is not added to any scene.

The sprite you can see is the effect of the codes

Player *layer1 = Player::node();
         CC_BREAK_IF(! layer1);
         scene->addChild(layer1,1);

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 … :slight_smile:

You should learn by the Simple Game code.
http://www.cocos2d-x.org/attachments/535/Cocos2dxSimpleGame-0.9.1.rar
It wrote for cocos 0.9.1 so it have something must change to run with 0.12
It a game with player and bullet fires. Hope you can find something useful