草庐IT

ios - 尝试从 GameLayer 访问类

coder 2024-01-12 原文

我有一个包含三个 Sprite 的自定义类“LinkWithNumber” 在更新的 GameLayer 上,我正在尝试测试 用于碰撞的 CGRectContainsRect 但是我在尝试访问类文件中的 Sprite 时遇到了问题(我没有太多经验所以很可能我搞砸了 :P)

我尝试了以下方法:

LinkWithNumber.h

@interface LinkWithNumber : SKSpriteNode <SKPhysicsContactDelegate>
{
SKSpriteNode *collide;
}

LinkWithNumber.m

@synthesize collide;

   //add collision object to the class
    collide = [[SKSpriteNode alloc]initWithColor:[SKColor blueColor]
                            ...blah blah as normal
   [self addChild:collide];
   collide.name = @"collide";

GameLayer.h

@class LinkWithNumber;
@interface GameScene : SKScene <SKPhysicsContactDelegate>
{
LinkWithNumber* twoSpritesWithParticlesBridge;
}
@property (nonatomic, strong)LinkWithNumber* twoSpritesWithParticlesBridge;

GameLayer.m

@synthesize twoSpritesWithParticlesBridge;

    -(void)addStaticLinkedSpriteWithParticles
{
    twoSpritesWithParticlesBridge =
    [[LinkWithNumber alloc]initWithlinkSpriteA:@"RoseMine06"
                                       spriteB:@"RoseMine06"
                             andPlistAnimation:@"need to create animations"
                                   distbetween:300
                                  hasParticles:YES
                                ParticlesNamed:@"Fire"];
      [self addChild:self->twoSpritesWithParticlesBridge];


    twoSpritesWithParticlesBridge.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: twoSpritesWithParticlesBridge.frame.size];



}

-(void)update:(CFTimeInterval)currentTime {

LinkWithNumber *currentSprite = 
(LinkWithNumber*)[self  childNodeWithName:@"collide"];

 //NSLog(@"currentSprite Name @%@", currentSprite); //gets nil

if (CGRectContainsRect(myShip02.frame,currentSprite.frame)) {
NSLog(@"Hit barrier can pass");
        }
}

Any help would be appreciated :)

如何定位您的类对象...解决方案,感谢 0x141E!

 //name it on setup inside your customCLass 
 //eg yourObject.name = @"collide";

   //Now in Gamelayer locate your object by recursive search
   //it will look for any object named @"//collide"
   //without the slashes it will only look on the game layer
   //but since we need to dig a bit further we need them!

 LinkWithNumber *currentSprite =
(LinkWithNumber*)[self  childNodeWithName:@"//collide"];
NSLog(@"LinkWithNumber is %@",NSStringFromClass([currentSprite class]));


    //do something with your object
    if (currentSprite.position.y >0 ) {
        NSLog(@"currentSprite Position %f",currentSprite.position.y);
    }

额外内容

Sknode Class ref for other search functions

How to enumerate all nodes

最佳答案

我看到两个问题...

  1. 您仅在根节点(在本例中为场景)中搜索名为“碰撞”的节点,但该节点是 LinkWithNumber 节点的子节点,而不是场景。要递归搜索整个节点树,使用@"//collide"
  2. 您正在将搜索结果转换为 LinkWithNumber 指针,但是 collide 是一个 SKSpriteNode 而不是 LinkWithNumber.

关于ios - 尝试从 GameLayer 访问类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997788/

有关ios - 尝试从 GameLayer 访问类的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  9. ruby - 有没有办法从 ruby​​ case 语句中访问表达式? - 2

    我想从then子句中访问c​​ase语句表达式,即food="cheese"casefoodwhen"dip"then"carrotsticks"when"cheese"then"#{expr}crackers"else"mayo"end在这种情况下,expr是食物的当前值(value)。在这种情况下,我知道,我可以简单地访问变量food,但是在某些情况下,该值可能无法再访问(array.shift等)。除了将expr移出到局部变量然后访问它之外,是否有直接访问caseexpr值的方法?罗亚附注我知道这个具体示例很简单,只是一个示例场景。 最佳答案

  10. ruby - 从外部访问类的实例变量 - 2

    我理解(我认为)Ruby中类变量和类的实例变量之间的区别。我想知道如何从该类外部访问该类的实例变量。从内部(即在类方法中而不是实例方法中),它可以直接访问,但是从外部,有没有办法做MyClass.class.[@$#]variablename?我没有任何具体原因要这样做,只是学习Ruby并想知道是否可行。 最佳答案 classMyClass@my_class_instance_var="foo"class上述yield:>>foo我相信Arkku演示了如何从类外部访问类变量(@@),而不是类实例变量(@)。我从这篇文章中提取了上述内

随机推荐