2

I am making a sprite kit game, and I am kind of a beginner in iOS development. In my game, I am using 1 SKScene as 1 level. Each level will have increased difficulty from the previous level. By making multiple SKScenes, I have noticed that I have to reuse the code of previous levels again and again with minor changes to raise the difficulty. Isn't there a better way to do this rather than creating multiple scenes and copying the same code again and again? Is there a way to reuse the repeated code? If I want to create many levels, this would become cumbersome, and seems to reflect a bad design.

4

2 回答 2

3

因此,面向对象编程的理论出现了,在您的情况下,特别是继承。OOP 专门用于当我们必须一遍又一遍地重用代码时,我们只需继承该类,然后我们可以在该子类中调用其方法,甚至可以根据自己的意愿覆盖或重载方法。

请查看有关如何进行继承的文档。在objective cu中可以继承一个类

 @interface 
 yourclassName : superclassName  

句法

于 2014-07-23T04:57:49.990 回答
3

如何将 SKScene 子类化为一个级别。然后就可以初始化 SKScene。

类.h

@interface 
myLevel :SKScene

-initWithYourDifferentLevelProperties:(NSInteger *)levelNumber;

类.m

@implementation

-initWithYourDifferentLevelProperties: (NSInteger *)levelNumber {

numberOfMonsters = levelNumber

}

希望这可以帮助。

于 2014-07-23T05:00:10.907 回答