让CCSprite在被触摸时跟随另一个CCSprite?

发布于 2024-12-21 22:11:47 字数 282 浏览 0 评论 0原文

我想要完成的是让我的CCSprite在接触另一个CCSprite时跟随它。现在我所说的跟随的意思是,假设有另一个 CCSprite 在屏幕上向上移动的动画。因此,如果这个精灵击中了我的主精灵,我的主精灵应该随之在屏幕上移动。这个另一个精灵将是一个平台,所以从技术上来说,最终我希望精灵位于另一个精灵之上,但它会沿着平台 CCSprite 的顶部移动,就好像平台承载着主精灵一样.

现在我知道如何进行碰撞检测部分并使其他精灵具有动画效果,但是我如何让我的平台“承载”我的主要 CCSprite 也有点像电梯的工作方式?

What I want to accomplish is to make my CCSprite follow another CCSprite when it is touching it. Now what I mean by follow is, lets say there is an animation of another CCSprite moving up the screen. So if this sprite hits my main sprite, my main sprite should move up the screen with it. This other sprite will be a platform, so technically in the end I would want the sprite to be on top of the other sprite but it would be moving along the top of the platform CCSprite as if the platform was carrying the main sprite.

Now I know how to do the collision detection part and make the other sprite animate but how would I just make my platform 'carry' my main CCSprite also kind of like how an elevator works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-12-28 22:11:47

我认为您可以放置​​标志来检查 update: 方法中的“何时遵循”条件,您可以根据第一个精灵的位置重新定位第二个精灵。

-(void)update:(CCTime)delta
{   
   if(condition == YES)
   {
      secondSprite.position = ccp ( firstSprite.position.x + xOffSet , firstSprite.position.y + yOffSet);
   }
}

根据您的代码尝试这个..基本逻辑在这里..

I think you can place flag to check the condition "when to follow" in update: method you can reposition the second sprite according to position of first Sprite.

-(void)update:(CCTime)delta
{   
   if(condition == YES)
   {
      secondSprite.position = ccp ( firstSprite.position.x + xOffSet , firstSprite.position.y + yOffSet);
   }
}

Try this according to your code.. Basic logic is here..

梦晓ヶ微光ヅ倾城 2024-12-28 22:11:47

您可以使用 CCFollow 操作来执行任何操作节点跟随任何其他节点。

You can use the CCFollow action to have any node follow any other node.

放手` 2024-12-28 22:11:47
  • 您可以在不使用动作的情况下移动第一个精灵,通过手动更新他在 nextFrame:(ccTime) dt 类函数中的位置,这样做可以很容易地根据精灵位置加上偏移量来更新另一个精灵位置

  • 另一种解决方案可以是从其父级中删除跟随者精灵,并将其添加为移动节点的子级,我认为这可以在没有闪烁和性能损失的情况下完成。

编辑:
另一种解决方案(也许是最好的)

一种很好的方法,它与动作配合得很好,因此您不需要手动安排移动

您可以向移动精灵添加一个属性(您需要从 CCSprite 子类化,如果你还没有这样做),为了保留对追随者的引用

@property (readwrite, nonatomic, assign) CCSprite *follower;
@property (readwrite, nonatomic) BOOL followerActive;

,然后在你的游戏初始化中,你可以创建两个精灵,并将它们添加为主层的子元素,然后将对象的弱引用添加到追随者

platform.follower = followerSprite;

,当你需要启用跟随 platform.followerActive = YES;

此时在移动精灵中,您可以覆盖 setPosition 属性

-(void) setPosition:(CGPoint)position { 

if(self.followerActive) {
self.follower.position = ccpAdd(位置, 偏移量);
}
[超级设置位置:位置];
}

  • You can move the first sprite without using action, by manually updating his position in a nextFrame:(ccTime) dt kinda function, doing so would be easy to update another sprite position based on your sprite position plus an offset to position it on top.

  • Another solution could be removing the follower sprite from its parent and adding as a child of the moving node, i think this can be done without flickering and performance loss.

edit:
another solution (maybe the best one)

a good approach, that works great with Actions so you dont need to manually schedule movements

You can add a property to the moving sprite (you need to subclass from CCSprite, if you havent already done so), to keep a reference to the follower

@property (readwrite, nonatomic, assign) CCSprite *follower;
@property (readwrite, nonatomic) BOOL followerActive;

then in your game inits, you can create both sprites, and add them as childs of your main layer, then you add the weak reference from your object to the follower

platform.follower = followerSprite;

and when you need to enable the follow platform.followerActive = YES;

at this point in the moving sprite, you can override the setPosition property

-(void) setPosition:(CGPoint)position { 

if(self.followerActive) {
self.follower.position = ccpAdd(position, offset);
}
[super setPosition:position];
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文