注册 登录
编程论坛 IPhone(iOS)开发

ccsprite touch 事件

qq236533067 发布于 2012-10-08 13:23, 1091 次点击
请问问下,为啥我的精灵无法移动?请指教。代码上传了。
程序代码:
//
//  GoodsSprite.m

//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "GoodsSprite.h"


@implementation GoodsSprite
-(id)init
{
    if(self=[super init])
    {
        GoodsArray=[[CCArray alloc]initWithCapacity:40];
        CCSprite *weapon=[CCSprite spriteWithFile:@"weapon.png"];
        weapon.scale=0.46f;
        [GoodsArray addObject:weapon];
        CCSprite *clothes=[CCSprite spriteWithFile:@"clothes.png"];
        clothes.scale=0.46f;
        [GoodsArray addObject:clothes];
        CGSize size = [[CCDirector sharedDirector] winSize];
        float x=205,y=size.height-70;
        for(int i=0;i<2;i++)
        {
            x+=50;
            CCSprite *temp=[CCSprite node];
            temp=[GoodsArray objectAtIndex:i];
            temp.position=ccp(x, y);
            [self addChild:temp z:0 tag:101+i];
            
        }
        
    }
    return self;
}
+(id)GoodsSpriteWithRect:(CGRect)rect spriteManager:(CCSprite *)manager
{
    id rtn = [[[self alloc] initWithTexture:manager.texture rect:rect] autorelease];
   
    return rtn;
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    if(![self containsTouchLocation:touch])
    {
        return NO;
    }
    return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];  
    touchPoint = [[CCDirector sharedDirector] convertToUI:CGPointMake(touchPoint.x, touchPoint.y)];  
    self.position = CGPointMake(touchPoint.x, touchPoint.y);
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
   
}
-(void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}
-(void)onExit
{
    [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onExit];
}
-(CGRect)rect
{
    return CGRectMake(-rect_.size.width / 2, -rect_.size.height / 2, rect_.size.width, rect_.size.height);
}
-(BOOL)containsTouchLocation:(UITouch *)touch
{
   
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}
-(void)dealloc
{
    [super dealloc];
}
@end

[attach]65347[/attach]

[ 本帖最后由 qq236533067 于 2012-10-22 16:14 编辑 ]
3 回复
#2
qq2365330672012-10-09 08:13
求大神帮助啊。。。是不是我touchbegan那个方法里的获取有问题?求帮助,求解答!!
#3
qq2365330672012-10-09 15:48
看来这板块没啥人啊。。。还好我自己想到办法解决了。。。
#4
foktime2012-11-17 23:16
1