0
我試圖做一個簡單的遊戲,在cocos2d在現在我有類似的東西移動2 CCSprite與UITouch
#import "GameplayLayer.h"
@implementation GameplayLayer
-(id)init {
self = [super init];
if (self != nil) {
CGSize screenSize = [CCDirector sharedDirector].winSize;
// właczenie obsługi dotyku
self.isTouchEnabled = YES;
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
}
else{
paddle1 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
paddle2 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
puck = [CCSprite spriteWithFile:@"krazekiPhone.png"];
}
//Polozenie i inicjalizacja paletki nr 1
[paddle1 setPosition:
CGPointMake(screenSize.width/2,
screenSize.height*0.17f)];
[self addChild:paddle1];
//Polozenie i inicjalizacja paletki nr 2
[paddle2 setPosition:
CGPointMake(screenSize.width/2,
screenSize.height*0.83f)];
[self addChild:paddle2];
//Polozenie i inicjalizacja krązka
[puck setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[self addChild:puck];
}
return self;
}
//onEnter
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
//onExit
- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
-(BOOL)containsTouch:(UITouch *)touch {
CGRect r=[paddle1 textureRect];
CGPoint p=[paddle1 convertTouchToNodeSpace:touch];
return CGRectContainsPoint(r, p);
}
-(BOOL)containsTouch2:(UITouch *)touch {
CGRect r=[paddle2 textureRect];
CGPoint p=[paddle2 convertTouchToNodeSpace:touch];
return CGRectContainsPoint(r, p);
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ([self containsTouch:touch]){
CCLOG(@"krarzek 1 tapniety");
isTouched1 = YES;
}
if ([self containsTouch2:touch]){
CCLOG(@"krarzek 2 tapniety");
isTouched2 = YES;
}
return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
if (isTouched1){
CGPoint newTouchLocation = [touch locationInView:touch.view];
newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
[paddle1 setPosition:newTouchLocation];
}
if (isTouched2){
CGPoint newTouchLocation = [touch locationInView:touch.view];
newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
[paddle2 setPosition:newTouchLocation];
}
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
if (isTouched1){
isTouched1=NO;
CCLOG(@"krarzek 1 zwolniony");
}
if (isTouched2){
isTouched2=NO;
CCLOG(@"krarzek 2 zwolniony");
}
}
@end
作品CCSprite的舉動,但是當我在同時觸及2 CCSprite,它們重疊本身! 我如何將它們分開移動? 對不起,我的英語和感謝您的幫助!
多點觸控已啓用?對你的UIWindow或UIView使用setMultipleTouchEnabled:是,然後爲要接收觸摸的圖層設置isTouchEnabled屬性。然後試試 – samfisher 2013-03-23 22:41:28
是的,我有:[glView setMultipleTouchEnabled:YES];在AppDelegate中 – pbeo 2013-03-23 22:45:21