【问题标题】:Sprites disappear after touch instead of after drag精灵在触摸后消失而不是在拖动后消失
【发布时间】:2013-02-13 10:31:40
【问题描述】:

在我目前正在尝试开发的游戏中,我正在尝试拖放精灵。这是我当前的代码:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//gameStat is a boolean I use for pause and play function
if(gameStat == TRUE)
{
    NSSet *allTouch = [event allTouches];
    UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector]convertToGL:location];

    //Swipe Detection - Beginning point
    beginTouch = location;
    touchflag = 0;

    for(int i = 0; i < [sprArray count]; i++)
    {
        CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
        if(CGRectContainsPoint([sprite boundingBox], location))
        {
            selectedSprite = sprite;
            //touchflag is an int, if its 1, it will move the sprite
            //spriteTouch is a boolean for checking if the sprite's been moved
            touchflag = 1;
            _spriteTouch = TRUE;
            break;
        }
    }
}}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
    NSSet *allTouch = [event allTouches];
    UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector]convertToGL:location];

    if(_spriteTouch == TRUE)
    {
        if(touchflag == 1)
        {
            //Should move the sprite from start location x and y to end location x and y
            selectedSprite.position = ccp(location.x, location.y);
        }
        else
        {
            for(int i = 0; i < [sprArray count]; i++)
            {
                CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
                if(CGRectContainsPoint([sprite boundingBox], location))
                {
                    selectedSprite = sprite;
                    touchflag = 1;
                    break;
                }
            }
        }
    }
}}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
    //End point of sprite after dragged
    NSSet *allTouch = [event allTouches];
    UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector]convertToGL:location];

    endTouch = location;
    posX = endTouch.x;

    //Minimum swipe length
    posY = ccpDistance(beginTouch, endTouch);

    if(_spriteTouch == TRUE)
    {
        if(selectedSprite != nil)
        {
            if(selectedSprite.tag == 1)
            {
                [self gameOver];
            }
            else if(selectedSprite.tag == 2)
            {
                if(countDown > 0)
                {
                    [self ClearGame];
                }
                else if(countDown == 0)
                {
                    [self gameOver];
                }
            }
        }
        selectedSprite = nil;
    }
    _spriteTouch = FALSE;
}}

精灵的拖放工作正常。我的问题是即使您只是触摸精灵,游戏也会进入 gameOver 或 ClearGame 方法。我需要告诉我的代码不要做任何事情,除非精灵被拖动,但我不知道该怎么做。我在这里错过了什么?

【问题讨论】:

  • 检查并计算位置位移,然后将条件放在位移值上。
  • 所以如果我没看错的话,你希望游戏在精灵被拖动时清除,但在精灵被触摸时结束?
  • 是的,这正是现在发生的情况。我有一种预感,我的方法都乱七八糟了,但我不知道应该按什么顺序制作它们,因为如果我将拖动方法放在触摸移动方法中,如果你不这样做,游戏就不会结束拖动它,但当你这样做时,你不会看到精灵移动。它只是说游戏通关了。
  • 如果不是这样,我怀疑我的 selectedSprite.position = ccp(....) 是问题的原因,但如果我尝试删除它,或者用拖动方法替换它,我获得错误的访问异常
  • @rptwsthi,我不确定我是否理解您的建议,抱歉。

标签: iphone objective-c cocos2d-iphone


【解决方案1】:

所以,在稍微弄乱了我的代码之后,我有点解决了我的问题。确实是因为我的代码混乱了。下面是它现在的样子:

//I declared a boolean called _spriteTouch at the top of my class
-(id)init
{
//Added this to my init method
_spriteTouch = FALSE;}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

if(touchflag == 1)
{
//set _spriteTouch to true then set the position of the sprite
_spriteTouch = TRUE;
[selectedSprite setPosition:location];
}}}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStatus == TRUE)
{
    //I removed the NSSet *touch, CGPoint location etc since I no longer need it here. Keep it if you're gonna use it somewhere in this method
    //Check if _spriteTouch is true, if it is then the sprite would do what it should accordingly
    if(_spriteTouch == TRUE)
    {
        if(selectedSprite != nil)
        {
            //do something
        }
        selectedSprite = nil;
    }
    _spriteTouch = FALSE;
}

}

差不多就是这样。我还删除了 endTouch、beginTouch、dragSprite 等,因为我不再需要它们了。我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多