Objective c CCSprite不知什么原因消失了

Objective c CCSprite不知什么原因消失了,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,触摸后,_gameball精灵不再可见,这是什么原因?我记录了它,gameball的isvisible是真的,接触点始终在我的CCNode的内容大小内 那是.m文件 现在是.h文件 #import "GameScene.h" @interface GameScene() @property (nonatomic,strong) CCSprite* gameBall; @end @implementation GameScene @synthesize gameBall = _gameB

触摸后,_gameball精灵不再可见,这是什么原因?我记录了它,gameball的isvisible是真的,接触点始终在我的CCNode的内容大小内


那是.m文件

现在是.h文件

#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 the 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
可能是你的问题。。尝试将其更改为:

CGPoint touchPoint = [touch locationInNode:self];
touchPoint = [self convertToNodeSpace:touchPoint];
-编辑-

好的,试试:

CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [self convertToNodeSpace:touchPoint];
-edit2-

刚刚注意到了
cgpointmake

CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[self parent] convertToNodeSpace:touchPoint];
可能不会做任何特殊的事情,但您应该保持代码的单调性,就像您使用了
\u gameball.position
然后也使用
\u gameball.position.y

因此,您可能希望将其更改为:

[_gameBall setPosition:CGPointMake(touchPoint.x, self.gameBall.position.y)];
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[_gameBall parent] convertToNodeSpace:touchPoint];
[_gameBall setPosition:ccp(touchPoint.x, _gameBall.position.y)];
-edit3-

这似乎对我有用:

_gameBall setPosition:CGPointMake(touchPoint.x, _gameBall.position.y)];
-最终编辑-

由于您使用的是cocos2d,因此也可以使用其CCToucheSBegind函数。。将其更改为:

[_gameBall setPosition:CGPointMake(touchPoint.x, self.gameBall.position.y)];
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[_gameBall parent] convertToNodeSpace:touchPoint];
[_gameBall setPosition:ccp(touchPoint.x, _gameBall.position.y)];

如果未注册触摸,则添加:
[self-setTouchEnabled:YES]到您的场景。

您是否检查了touchPoint.x值?是的,我记录了它,它显示了有效的数据。使用以下命令:CGPoint location=[self convertTouchToNodeSpace:touch]@沙克尔。这里没有convertTouchToNodeSpace,只有convertToNodeSpace。我用self得到了这个方法。您能同时共享.h和.m文件吗?你的文件是什么类型的?同样的,触摸后它就消失了。就像它没有在新的位置被重新绘制一样。试试第二个,看看它是否有效,因为我有一种感觉,当它一点一点地出现在屏幕上时,它会离开屏幕。好吧,现在它没有消失,但它似乎将精灵X轴的位置设置为0,而不是接触点X 1。那么我们走对了,因为:这意味着你的代码不是让它不可见,而是把它放在屏幕外。虽然我想知道为什么。让我再拍一次,5分钟后回来。如果我使用cpp(x,y),它不会离开屏幕,但我必须使用接触点坐标。
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSArray* touchArray = [touches allObjects];
    for (UITouch* touch in touchArray)
    {
        CGPoint touchPoint = [touch locationInView:[touch view]];
        touchPoint = [_gameBall convertToNodeSpace:touchPoint];
        [_gameBall setPosition:ccp(touchPoint.x, _gameBall.position.y)];
        break;
    }
}