Ios 如何将不同类的精灵添加到场景中?
我创建了一个名为“level1Layout.m”和“level1Layout.h”的新类。这些是分类代码 level1Layout.m level1Layout.h 请有人帮忙。 当我将“testMethod”更改为实例时,当它崩溃时,我会在控制台中得到它Ios 如何将不同类的精灵添加到场景中?,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我创建了一个名为“level1Layout.m”和“level1Layout.h”的新类。这些是分类代码 level1Layout.m level1Layout.h 请有人帮忙。 当我将“testMethod”更改为实例时,当它崩溃时,我会在控制台中得到它 2015-01-03 15:48:51.831 SpellCastTest2.5[8559:609325] -[level1Layout runAddLevel1Layout]: unrecognized selector sent to i
2015-01-03 15:48:51.831 SpellCastTest2.5[8559:609325] -[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0
2015-01-03 15:48:51.850 SpellCastTest2.5[8559:609325] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104d86f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104a1fbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000104d8e04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000104ce627c ___forwarding___ + 988
4 CoreFoundation 0x0000000104ce5e18 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001045d92b4 __NSFireTimer + 83
6 CoreFoundation 0x0000000104ceef64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7 CoreFoundation 0x0000000104ceeb25 __CFRunLoopDoTimer + 1045
8 CoreFoundation 0x0000000104cb1e5d __CFRunLoopRun + 1901
9 CoreFoundation 0x0000000104cb1486 CFRunLoopRunSpecific + 470
10 GraphicsServices 0x000000010b5f99f0 GSEventRunModal + 161
11 UIKit 0x0000000105329420 UIApplicationMain + 1282
12 SpellCastTest2.5 0x00000001044f1783 main + 115
13 libdyld.dylib 0x00000001074cc145 start + 1
)
libc++
我找到了导致它的原因,就是这一部分
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(runLevel1Layout)
userInfo:nil
repeats:NO];
您的崩溃日志中描述的问题起源于:
+(void) testMethod{
// ...
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(addSprite)
userInfo:nil
repeats:NO];
}
注意testMethod
之前的+
:这是一个类方法。因此,您正在安排计时器调用+[level1Layout addSprite]
,但是addSprite
是一个实例方法(用-
声明):-[level1Layout addSprite]
)
因此,您需要将testMethod
声明为实例方法,或者更改目标self
,以指向level1Layout
的特定实例
请参阅。阅读您的代码的人的勇气哈哈,很抱歉发布了这么多lol@BeguerajYou不应该在Sprite Kit中使用NSTimer:谢谢,尽管Sprite从未出现,但计时器仍然更容易键入@事实上,你告诉我要用的新定时器从未被调用过,也从未做过它应该做的事。。代码跳过了它或者@learncos2dok,所以我把它改成了一个实例,然后它又崩溃了,我编辑了我的帖子并加入了一些东西。你看到新的错误了吗?我打赌你一定能弄明白。这与第一个错误非常相似。我确实犯了,我想我已经发现了。。我只是将“runAddLevel1Layout”更改为实例,而不是类方法。但是精灵从来没有出现过。。但是没有错误
2015-01-03 15:48:51.831 SpellCastTest2.5[8559:609325] -[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0
2015-01-03 15:48:51.850 SpellCastTest2.5[8559:609325] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104d86f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104a1fbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000104d8e04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000104ce627c ___forwarding___ + 988
4 CoreFoundation 0x0000000104ce5e18 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001045d92b4 __NSFireTimer + 83
6 CoreFoundation 0x0000000104ceef64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7 CoreFoundation 0x0000000104ceeb25 __CFRunLoopDoTimer + 1045
8 CoreFoundation 0x0000000104cb1e5d __CFRunLoopRun + 1901
9 CoreFoundation 0x0000000104cb1486 CFRunLoopRunSpecific + 470
10 GraphicsServices 0x000000010b5f99f0 GSEventRunModal + 161
11 UIKit 0x0000000105329420 UIApplicationMain + 1282
12 SpellCastTest2.5 0x00000001044f1783 main + 115
13 libdyld.dylib 0x00000001074cc145 start + 1
)
libc++
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(runLevel1Layout)
userInfo:nil
repeats:NO];
+(void) testMethod{
// ...
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(addSprite)
userInfo:nil
repeats:NO];
}