代码如下
这里需要添加协义
CCTargetedTouchDelegate
#import <Foundation/Foundation.h>
#import "cocos2d.h"
typedef enum {
CardTypeHearts = 1,
CardTypeSpades = 2,
CardTypeBox=3,
CardTypeFlower=4,
CardTypeKingBig=5,
CardTypeKingSmall=6
} CardType;
typedef enum {
CardSizeTypeBig = 1,
CardSizeTypeSmall = 2,
CardSizeTypeIn=3
} CardSizeType;
@interface Card : CCSprite <CCTargetedTouchDelegate>{
NSInteger _size;
CardType _type;
ccColor3B _ccColor3B;
CardSizeType _cardSizeType;
Boolean _isTouchDray;
CCSprite *cardTypeCCSprite;
CCSprite *cardSize;
CCSprite *cardTypeCCSprite1;
CCSprite *cardSize1;
}
@property(assign)NSInteger size;
@property(assign)CardType type;
@property(assign)Boolean isTouchDray;
-(id)initPoker:(CardType)cardType PokerSize:(NSInteger)pokerSize CardSizeType:(CardSizeType)pokerType;
-(void)LoadSizeType :(CardType)cardType PokerSize:(NSInteger)pokerSize;
-(id)initWithBackgroundSpriteFrameNameBig;
-(id)initWithBackgroundSpriteFrameNameIn;
-(id)initWithBackgroundSpriteFrameNameSmall;
-(void)actionTouchDray;
-(void)actionTouchDrop;
@end
然后在实现在文件里
-(void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
-(void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
// CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
// touchoffset=touchLocation;
return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
NSLog(@"%f ,%f",touchLocation.x,touchLocation.y);
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
NSLog(@"%f ,%f",touchLocation.x,touchLocation.y);
}
onEnter 运行在init 这前
onExit