如何在碰撞时显示文字?

发布于 2024-10-09 10:33:37 字数 195 浏览 0 评论 0原文

需要快速帮助。我正在创建我的第一个游戏 Cocos 2D 和 Box 2D,并且需要有关如何在碰撞期间显示文本的帮助。标准非常简单。我需要使用石头和弹弓击中从上面掉落的物体。每个对象都有自己的要点和特征。对于其中一个我需要实现的,如果石头摧毁了超过 3 个物体,“组合”一词应该出现在屏幕上并逐渐淡出。将热切等待您的回复和建议。

问候,

卡蒂克

Need a Quick Help. I'm creating my first game Cocos 2D and Box 2D and would require help on how to display text during collision. Criteria is very simple. Using a stone and a slingshot i need to hit the objects falling from above. Each object has its own points and features. For one of them i need to implement, if the stone destroys more than 3 objects, the word "combo" should come up on screen and gradually fade out. Will be eagerly waiting for your reply and suggestions.

Regards,

Karthik

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

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

发布评论

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

评论(2

云柯 2024-10-16 10:33:38

例如,在 cgintersectect 中检测更新中的碰撞,您必须编写如下文本。

示例代码:-

if (CGRectIntersectsRect(projectileRect, targetRect)) {
                [targetsToDelete addObject:target];
                CCMenuItem *pause_menu = [CCMenuItemImage itemFromNormalImage:@"pause.png" selectedImage:@"pause.png" target:self selector:@selector(pauseGame:)];
                CCMenu *menu = [CCMenu menuWithItems: pause_menu, nil];
                menu.position = ccp(460, 15);
                [self addChild:menu ];
            }   

for example in cgintersectrect is detect your collision in update there you have to write text like these.

Example code:-

if (CGRectIntersectsRect(projectileRect, targetRect)) {
                [targetsToDelete addObject:target];
                CCMenuItem *pause_menu = [CCMenuItemImage itemFromNormalImage:@"pause.png" selectedImage:@"pause.png" target:self selector:@selector(pauseGame:)];
                CCMenu *menu = [CCMenu menuWithItems: pause_menu, nil];
                menu.position = ccp(460, 15);
                [self addChild:menu ];
            }   
殤城〤 2024-10-16 10:33:38

看起来相当简单:

// use this to keep track of how many stones are destroyed, 
// every time a stone is destroyed, increment it by 1
int count = 0;

要显示“组合”,您需要使用图形或标签 - 这在 cocos2d 示例中有所介绍,因此我不会在这里解释如何显示它。

从这里开始,在“tick”回调中,只需检查“count”的值,如果它大于或等于“3”(您的“神奇”数字),则在您的“组合”节点上运行以下操作序列。

CCFadeIn *fadeIn = [CCFadeIn actionWithDuration: 0.25f];
CCFadeOut *fadeOut = [CCFadeOut actionWithDuration: 0.0125f];
CCDelayTime *delay = [CCDelayTime actionWithDuration: 0.5f];
[node runAction: [CCSequence actions: fadeIn, delay, fadeOut, nil];

这组操作将在四分之一秒内淡出节点,等待半秒,然后在八分之一秒内淡出。您可以根据需要调整代码的时间。当计数器 > 时,您可以执行一些其他操作。 3 是将“组合”文本重新定位到最后一个被销毁的项目的位置,或者随机化它的位置,这样它并不总是在同一个位置,等等...

示例代码中的“节点”对象是保留下来的 CCSprite 或 CCLabel添加到场景中,但只是切换其可见性(因为该项目很可能“经常”出现在场景中,因此最好将其保留在场景中并更改其可见性,而不是每次创建一个新项目并销毁它 -如果它是不常见的物品,只需创建一个新物品,然后销毁它......

Seems fairly straightforward and simple:

// use this to keep track of how many stones are destroyed, 
// every time a stone is destroyed, increment it by 1
int count = 0;

To display the "combo", you would want to use a graphic or a label - this is covered in the cocos2d examples, so I won't explain how to display it here.

From here, in your "tick" callback, just check the value of "count", if it is greater than or equal to "3" (your "magic" number) then run the following action sequence on your "combo" node.

CCFadeIn *fadeIn = [CCFadeIn actionWithDuration: 0.25f];
CCFadeOut *fadeOut = [CCFadeOut actionWithDuration: 0.0125f];
CCDelayTime *delay = [CCDelayTime actionWithDuration: 0.5f];
[node runAction: [CCSequence actions: fadeIn, delay, fadeOut, nil];

This set of actions will Fade the node in over a quarter second, wait for half a second, then fade it out over an eighth of a second. You can adjust timing as necessary, for your code. Some additional things you can do, when the counter is > 3 is reposition the "combo" text to the position of the last item destroyed, or randomize it's position so it's not always in the same place, etc ...

The "node" object in the example code is a CCSprite or CCLabel that remains added to your scene, but simply has it's visibility toggled (as this item will appear in the scene more than likely "often" it's better to leave it in the scene and change it's visibility rather than create a new one and destroy it each time - if it's an infrequent item, just create a new one, and destroy it ...

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