Ios 为什么[SKAction runBlock:^(void){}]永远不起作用?
我正在尝试制作无限数量的硬币,这些硬币一个接一个地掉落(由Ios 为什么[SKAction runBlock:^(void){}]永远不起作用?,ios,objective-c,sprite-kit,skscene,Ios,Objective C,Sprite Kit,Skscene,我正在尝试制作无限数量的硬币,这些硬币一个接一个地掉落(由waitforduration:方法生成相等的空格),由于某些原因,我无法理解,[NSTread sleepForTimeInterval:在sprite工具包中无法正常工作(第一枚硬币和第二枚硬币之间的间距总是不同于其他相等的间距),因此我尝试使用此代码,但事实是,它从来都不起作用,我做错了什么?然而,我的代码与dispatch\u queue\u t fallingCoinsQueue=dispatch\u queue\u creat
waitforduration:
方法生成相等的空格),由于某些原因,我无法理解,[NSTread sleepForTimeInterval:
在sprite工具包中无法正常工作(第一枚硬币和第二枚硬币之间的间距总是不同于其他相等的间距),因此我尝试使用此代码,但事实是,它从来都不起作用,我做错了什么?然而,我的代码与dispatch\u queue\u t fallingCoinsQueue=dispatch\u queue\u create(“falling coins”,nil)一起工作;
dispatch_async(fallingCoinsQueue,^{///做任何事情});
但是我想使用SKAction runBlock^(void){}
而不是那样,任何帮助都非常感谢
@interface MyScene()
{
SKTexture* goldenCoin;
SKTexture* blackCoin;
}
@property (nonatomic,strong) SKSpriteNode * randomCoinNode;
@end
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self AddRightColumnCoins:self.size];
}
return self;
}
-(void)AddRightColumnCoins:(CGSize)size{
__block int counter = 0;
_randomCoinNode = [SKSpriteNode new];
SKAction *run = [SKAction runBlock:^(void){
NSArray * coinArray= [[NSArray alloc] initWithObjects:goldenCoin,blackCoin,nil];
NSUInteger randomIndex = arc4random() % [coinArray count];
_randomCoinNode =[SKSpriteNode spriteNodeWithTexture:coinArray[randomIndex]];
_randomCoinNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: _randomCoinNode.size.width/2];
CGFloat x = self.view.frame.size.width - ( _randomCoinNode.frame.size.width - 10 );
CGFloat y = self.view.frame.size.height;
_randomCoinNode.position = CGPointMake(x , y);
_randomCoinNode.physicsBody.dynamic = NO;
if( _randomCoinNode.texture == goldenCoin){
_randomCoinNode.physicsBody.categoryBitMask = goldenCoinCategory;
}else{
_randomCoinNode.physicsBody.categoryBitMask = blackCoinCategory;
}
_randomCoinNode.physicsBody.contactTestBitMask = flappyCategory;
counter++;
[self addChild: _randomCoinNode];
_fall = [SKAction moveToY:-50.0f duration:2];
[_randomCoinNode runAction:_fall];
}];
SKAction *wait = [SKAction waitForDuration:0.6];
SKAction *sequence = [SKAction sequence:@[run, wait]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
[_randomCoinNode runAction: repeat];
}
就我个人而言,当涉及到一些事情时,我可能会以不同的方式来做这件事,比如命名硬币纹理,或者使用属性与实例变量等。但是,为了保留我的示例,与您的代码类似,您可以这样做(复制并粘贴它以查看其工作原理): 关于您的代码:
,在最好的情况下将存储对添加到场景中的最后一枚硬币的引用。这没有多大意义,我猜这不是您想要的\u randomCoinNode
- 除了初始化器、反初始化器和重写的访问器方法(getter和setter)之外,您应该使用访问器方法或点语法来访问实例变量(根据docs的建议)。这意味着,在您的方法中,您应该使用
而不是self.randomCoinNode
\u randomCoinNode
- 我在评论中已经提到的其他问题(强引用循环和在块内捕捉自我)
[self addChild:_randomconnode]代码>非常感谢,我会尽快尝试。只是想知道为什么硬币。physicsBody
已经初始化了两次。谢谢你@Whirlwind@ConnorT复制/粘贴错误:)。您可以删除一条语句而无需担心。谢谢你指出这一点。
#import "GameScene.h"
@interface GameScene()
{
SKTexture* goldenCoin;
SKTexture* blackCoin;
}
@end
@implementation GameScene
-(instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"coins"];
goldenCoin = [atlas textureNamed:@"goldenCoin"];
blackCoin = [atlas textureNamed:@"blackCoin"];
}
return self;
}
-(void)didMoveToView:(SKView *)view{
//use withRange: parameter if you want to randomize a delay
SKAction *wait = [SKAction waitForDuration:1];
__weak typeof(self) weakSelf = self;
SKAction *block = [SKAction runBlock:^{
SKSpriteNode *coin = [weakSelf spawnRandomCoin];
[weakSelf addChild:coin];
}];
SKAction *sequence = [SKAction sequence:@[wait, block]];
SKAction *loop = [SKAction repeatActionForever:sequence];
//access this key later to stop spawning
[self runAction:loop withKey:@"spawning"];
}
-(SKSpriteNode*)spawnRandomCoin {
NSArray * coinArray= [[NSArray alloc] initWithObjects:goldenCoin,blackCoin,nil];
NSUInteger randomIndex = arc4random() % [coinArray count];
NSLog(@"random index : %lu", randomIndex);
//Create and setup a coin
SKSpriteNode *coin = [SKSpriteNode spriteNodeWithTexture:coinArray[randomIndex]];
coin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: coin.size.width/2.0f];
CGFloat x = self.view.frame.size.width - ( coin.frame.size.width - 10.0f );
CGFloat y = self.view.frame.size.height;
coin.position = CGPointMake(x , y);
coin.physicsBody.dynamic = NO;
//setup coin's category here
if (coin.texture == blackCoin){
}else{
}
//setup moving action
SKAction *move = [SKAction moveToY:-coin.size.width duration:5];
SKAction *moveAndRemove = [SKAction sequence:@[move, [SKAction removeFromParent]]];
[coin runAction:moveAndRemove withKey:@"moving"];
return coin;
}
@end