[Cpp] view plaincopyprint?
There are multiple genie in the scenario, so you need to move them to an appropriate location. A simple move may result in overlapping sprite regions. This problem can be solved by slightly modifying the ccTouchBegan function, each time you move the sprite under the current ccTouchBegan click. You need to enable the ccTouchBegan and ccTouchMoved proxies. Otherwise, the setTouchEnabled (true) alone is ineffective. Let's check the Code directly.
[Cpp]
Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
CC_BREAK_IF (! CCLayer: init ());
CCMenuItemImage * pCloseItem = CCMenuItemImage: create (
"CloseNormal.png ",
"CloseSelected.png ",
This,
Menu_selector (HelloWorld: menuCloseCallback ));
CC_BREAK_IF (! PCloseItem );
PCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20,500 ));
CCMenuItemImage * pSaveItem = CCMenuItemImage: create (
"Save_1.png ",
"Save_2.png ",
This,
Menu_selector (HelloWorld: menuSaveCallback ));
CC_BREAK_IF (! PSaveItem );
PSaveItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-100,100 ));
CCMenu * pMenu = CCMenu: create (pCloseItem, pSaveItem, NULL );
PMenu-> setPosition (CCPointZero );
CC_BREAK_IF (! PMenu );
This-> addChild (pMenu, 1 );
CCSize s = CCDirector: sharedDirector ()-> getWinSize ();
CCSprite * bg = CCSprite: create ("bg.png ");
CC_BREAK_IF (! Bg );
Bg-> setPosition (ccp (s. width/2, s. height/2 ));
This-> addChild (bg );
//////////////////////////////////////// //////////////////////////////////
TagCount = 0;
CCSprite * sprite = CCSprite: create ("1.png ");
CC_BREAK_IF (! Sprite );
Sprite-> setPosition (ccp (100,200 ));
This-> addChild (sprite, 1, ++ TagCount );
CCSprite * sprite2 = CCSprite: create ("2.png ");
CC_BREAK_IF (! Sprite2 );
Sprite2-> setPosition (CP (500,200 ));
This-> addChild (sprite2, 2, ++ TagCount );
CCDirector * pDirector = CCDirector: shareddire ();
PDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
SetTouchEnabled (true );
BRet = true;
} While (0 );
Return bRet;
}
Bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent)
{
Int tag;
For (tag = 1; tag <= TagCount; tag ++)
{
CCSprite * sprite = (CCSprite *) this-> getChildByTag (tag );
CCPoint touchPoint = pTouch-> getLocationInView ();
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
CCRect rc1 = sprite-> boundingBox ();
If (rc1.containsPoint (touchPoint ))
{
PSprite = sprite;
Return true;
}
}
Return false;
}
Void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint beginPoint = pTouch-> getLocationInView ();
BeginPoint = CCDirector: sharedDirector ()-> convertToGL (beginPoint );
CCPoint endPoint = pTouch-> getpreviuslocationinview ();
EndPoint = CCDirector: sharedDirector ()-> convertToGL (endPoint );
CCPoint offSet = ccpSub (beginPoint, endPoint );
CCPoint toPoint = ccpAdd (pSprite-> getPosition (), offSet );
PSprite-> setPosition (toPoint );
}
Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
CC_BREAK_IF (! CCLayer: init ());
CCMenuItemImage * pCloseItem = CCMenuItemImage: create (
"CloseNormal.png ",
"CloseSelected.png ",
This,
Menu_selector (HelloWorld: menuCloseCallback ));
CC_BREAK_IF (! PCloseItem );
PCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20,500 ));
CCMenuItemImage * pSaveItem = CCMenuItemImage: create (
"Save_1.png ",
"Save_2.png ",
This,
Menu_selector (HelloWorld: menuSaveCallback ));
CC_BREAK_IF (! PSaveItem );
PSaveItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-100,100 ));
CCMenu * pMenu = CCMenu: create (pCloseItem, pSaveItem, NULL );
PMenu-> setPosition (CCPointZero );
CC_BREAK_IF (! PMenu );
This-> addChild (pMenu, 1 );
CCSize s = CCDirector: sharedDirector ()-> getWinSize ();
CCSprite * bg = CCSprite: create ("bg.png ");
CC_BREAK_IF (! Bg );
Bg-> setPosition (ccp (s. width/2, s. height/2 ));
This-> addChild (bg );
//////////////////////////////////////// //////////////////////////////////
TagCount = 0;
CCSprite * sprite = CCSprite: create ("1.png ");
CC_BREAK_IF (! Sprite );
Sprite-> setPosition (ccp (100,200 ));
This-> addChild (sprite, 1, ++ TagCount );
CCSprite * sprite2 = CCSprite: create ("2.png ");
CC_BREAK_IF (! Sprite2 );
Sprite2-> setPosition (CP (500,200 ));
This-> addChild (sprite2, 2, ++ TagCount );
CCDirector * pDirector = CCDirector: shareddire ();
PDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
SetTouchEnabled (true );
BRet = true;
} While (0 );
Return bRet;
}
Bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent)
{
Int tag;
For (tag = 1; tag <= TagCount; tag ++)
{
CCSprite * sprite = (CCSprite *) this-> getChildByTag (tag );
CCPoint touchPoint = pTouch-> getLocationInView ();
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
CCRect rc1 = sprite-> boundingBox ();
If (rc1.containsPoint (touchPoint ))
{
PSprite = sprite;
Return true;
}
}
Return false;
}
Void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint beginPoint = pTouch-> getLocationInView ();
BeginPoint = CCDirector: sharedDirector ()-> convertToGL (beginPoint );
CCPoint endPoint = pTouch-> getpreviuslocationinview ();
EndPoint = CCDirector: sharedDirector ()-> convertToGL (endPoint );
CCPoint offSet = ccpSub (beginPoint, endPoint );
CCPoint toPoint = ccpAdd (pSprite-> getPosition (), offSet );
PSprite-> setPosition (toPoint );
}