objective c - CCSprite disappears for unknown reason -
-(void)touchbegan:(uitouch*)touch withevent:(uievent*)event { cgpoint touchpoint = [touch locationinnode:self]; [_gameball setposition:cgpointmake(touchpoint.x,self.gameball.position.y)]; }
after touch _gameball sprite not visible anymore, reason why happens?i logged , isvisible gameball true , touchpoint within content size of ccnode.
#import "gamescene.h" @interface gamescene() @property (nonatomic,strong) ccsprite* gameball; @end @implementation gamescene @synthesize gameball = _gameball; +(gamescene*)gameinstance { return [[self alloc]init]; } -(id)init { self = [super init]; if (self) { //implement //user interaction self.userinteractionenabled = yes; //create ball _gameball = [ccsprite spritewithimagenamed:@"small_blue_ball.png"]; _gameball.position = ccp(0.5f,0.1f); _gameball.positiontype = ccpositiontypenormalized; [self addchild:_gameball]; } return self; } -(void)onenter { [super onenter]; } -(void)onexit { [super onexit]; } -(void)touchbegan:(uitouch *)touch withevent:(uievent *)event { cgpoint touchpoint = [touch locationinnode:self]; touchpoint = [self converttonodespace:touchpoint]; [_gameball setposition:cgpointmake(touchpoint.x, self.gameball.position.y)]; nslog(@"%f , %f",_gameball.position.x,_gameball.position.y); } -(void)touchmoved:(uitouch *)touch withevent:(uievent *)event { } @end
that .m file
now .h file
#import "cocos2d-ui.h" #import "cocos2d.h" @interface introscene : ccscene +(ccscene*)scene; @end
cgpoint touchpoint = [touch locationinnode:self]; touchpoint = [self converttonodespace:touchpoint];
might problem.. try changing to:
cgpoint touchpoint = [touch locationinview:[touch view]]; touchpoint = [self converttonodespace:touchpoint];
-edit-
ok, try:
cgpoint touchpoint = [touch locationinview:[touch view]]; touchpoint = [[self parent] converttonodespace:touchpoint];
-edit2-
just noticed cgpointmake
:
[_gameball setposition:cgpointmake(touchpoint.x, self.gameball.position.y)];
might not special, should keep code monotone, in if used _gameball.position
use _gameball.position.y
so might want change to:
_gameball setposition:cgpointmake(touchpoint.x, _gameball.position.y)];
-edit3-
this seems work me:
cgpoint touchpoint = [touch locationinview:[touch view]]; touchpoint = [[_gameball parent] converttonodespace:touchpoint]; [_gameball setposition:ccp(touchpoint.x, _gameball.position.y)];
-final edit-
since using cocos2d, use cctouchesbegan function.. change to:
-(void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event { nsarray* toucharray = [touches allobjects]; (uitouch* touch in toucharray) { cgpoint touchpoint = [touch locationinview:[touch view]]; touchpoint = [_gameball converttonodespace:touchpoint]; [_gameball setposition:ccp(touchpoint.x, _gameball.position.y)]; break; } }
if doesn't register touches, add: [self settouchenabled:yes];
scene.
Comments
Post a Comment