【问题标题】:grouping sprites and moving them?分组精灵并移动它们?
【发布时间】:2011-03-31 15:37:28
【问题描述】:

我想对从数组中调用的精灵进行分组,以便将它们作为一个组移动。有人告诉我使用 cocosnode,但我很确定他的意思是 ccnode。到目前为止,这是我的代码:

    sprites1 = (CCSprite *)[c1array objectAtIndex:0];
    sprites2 = (CCSprite *)[c1array objectAtIndex:1];
    sprites3 = (CCSprite *)[c1array objectAtIndex:2];
    sprites4 = (CCSprite *)[c1array objectAtIndex:3];
    sprites5 = (CCSprite *)[c1array objectAtIndex:4];
    sprites6 = (CCSprite *)[c1array objectAtIndex:5];
    sprites7 = (CCSprite *)[c1array objectAtIndex:6];
    sprites8 = (CCSprite *)[c1array objectAtIndex:7];

    column1 = [CCNode node];
    [column1 addChild:sprites1];
    [column1 addChild:sprites2];
    [column1 addChild:sprites3];
    [column1 addChild:sprites4];
    column1.position = ccp(0,0);
    [self addChild:column1];

    column2 = [CCNode node];
    [column2 addChild:sprites5];
    [column2 addChild:sprites6];
    [column2 addChild:sprites7];
    [column2 addChild:sprites8];
    column2.position = ccp(30,0);
    [self addChild:column2];

//ccotouchmoved code
column1.anchorPoint = ccp(touchLocation.x,touchLocation.y);
if (CGRectContainsPoint(c1,touchLocation)) {
        touchLocation.x = column1.position.x;
        column1.position = ccp(touchLocation.x,touchLocation.y);
}

如何使 ccnode 顺利移动。它跳了很多,但我想要一个平稳的上下过渡。

xxx

xxx

xxx

x 是我的精灵,我正在将我的 x 作为一整列上下移动,并且需要能够将我的 x 作为一整行移动,一旦它离开屏幕顶部,我需要它重新出现在屏幕的另一侧,反之亦然,左右相同。

【问题讨论】:

  • 崩溃时出现什么错误?

标签: objective-c cocos2d-iphone grouping sprite


【解决方案1】:

它崩溃的可能原因是nil中的Sprite5、Sprite6、Sprite7、Sprite8之一。并且拒绝给CCNode添加nil children。

在 touchBegan 方法中:

CGPoint touchLocation = ...;
CGPoint referencePoint = ccpSub(touchLocation, myNode.position);
//keep reference point somewhere

在 TouchMoved 方法中:

CGPoint touchLocation = ...;
myNode.position = ccpSub(touchLocation, referencePoint);

【讨论】:

  • @lins314159,@Andrew,别担心我得到它的工作忘记添加精灵等于数组中的对象。只需要整理运动。
  • @Andrew,此时当您单击该位置上方时位于底部的 ccnode 位置,触摸位置等于 ccnode 的 y 值,因此它会跳转但我想要它以便当您在该点单击 ccnode 不会跳转到该位置。
  • @Danger:将节点的锚点更改为与您的手指重合。或者将初始偏差存储在某个变量中并在移动时使用它
  • @Andrew,我已经尝试过了,但它仍然存在同样的问题,我将编辑我的代码以显示我目前拥有的内容,你能否提供一个我可能误解的例子。
  • @Andrew,我试过这个,但它没有用,仍然是同样的问题。我试图做的是获得滚动效果,就像您在网页上像滚动条一样上下拖动时,但就像在网页上单击滚动条上方一样,滚动条会跳转到该位置。当我单击 ccnode.position 上方时,我遇到了这个问题。我想要这样,但这与层有关dk101.net/2010/11/30/implementing-page-scrolling-in-cocos2d
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2015-05-20
  • 2017-05-19
  • 2017-05-27
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
相关资源
最近更新 更多