Objective C - 通过点击精灵生成字符串,这可能吗?

发布于 2024-10-03 09:19:22 字数 114 浏览 1 评论 0原文

我将描述这个问题,我的精灵都是字母表中的字母,我想知道当你触摸这些字母形成一个单词时我该如何做到这一点,我可以生成一个该单词的字符串,并通过它与一个字符串进行比较有一个plist。我需要任何可以帮助我的想法,谢谢。

I will describe the problem, I have sprites that are all letters of the alphabet, and I wonder how I can do that when you touch the letters form a word and I can generate a string that word and through it to compare with a string that have a plist. I need any idea that might help me, thank you.

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-10-10 09:19:22

是的,这是可能的。测试触摸的一种方法是使用 CCTouchDispatcher。

概述

  1. 确定哪个类将监视字母精灵的触摸。
  2. 使该类成为 CCTargetedTouchDelegate 的委托。
  3. 将代码添加到类以向 CCTouchDispatcher 注册。
  4. 向类中添加代码以取消向 CCTouchDispatcher 注册。
  5. 将触摸回调方法添加到您的类中。在触摸回调方法中,您必须添加代码来确定哪个精灵被触摸。

从调度程序回调方法注册和取消注册

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];

以实现用于

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

测试是否触摸 Sprite 的示例代码

- (BOOL) isTouch:(UITouch *)touch InSprite:(CCSprite *)sprite 
{
    CGPoint touchLocation = [touch locationInView: [touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];

    CGPoint localLocation = [sprite convertToNodeSpace:touchLocation];
    CGRect spriteRect = [sprite textureRect];
    spriteRect.origin = CGPointZero;

    if(CGRectContainsPoint(spriteRect, localLocation))
    {
        return YES;
    }

    return NO;
}

Yes, this is possible. One way to test for touches is by using the CCTouchDispatcher.

Overview

  1. Determine which class will monitor for touches of your letter sprites.
  2. Make the class a delegate of CCTargetedTouchDelegate.
  3. Add code to the class to register with the CCTouchDispatcher.
  4. Add code to the class to unregister with the CCTouchDispatcher.
  5. Add the touch callback methods to your class. In the touch callback methods, you must add code for determining which sprite was touched.

Register and unregister from Dispatcher

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];

CallBack Methods To Implement

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

Example Code For Testing If Touch In Sprite

- (BOOL) isTouch:(UITouch *)touch InSprite:(CCSprite *)sprite 
{
    CGPoint touchLocation = [touch locationInView: [touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];

    CGPoint localLocation = [sprite convertToNodeSpace:touchLocation];
    CGRect spriteRect = [sprite textureRect];
    spriteRect.origin = CGPointZero;

    if(CGRectContainsPoint(spriteRect, localLocation))
    {
        return YES;
    }

    return NO;
}
素衣风尘叹 2024-10-10 09:19:22

也许使用 CCMenuItem 会更简单(它也可以从精灵创建),因为它已经是可触摸的。您所要做的就是指定当 CCMenuItem 被触摸时将调用的函数。

看看官方的编程指南:
http://www.cocos2d-iphone.org/wiki/doku .php/prog_guide:lesson_3._menus_and_scenes

Maybe it will be more simple to use CCMenuItem (it can also be created from sprite) because it's already touchable. Everything you have to do is to specify a function will be called, when CCMenuItem is touched.

Take a look at the official programming guide:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_3._menus_and_scenes

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