屏幕上的Objective-C随机位置无法正常工作(SpriteKit) [英] Objective-C random position on screen not working properly ( SpriteKit )

查看:52
本文介绍了屏幕上的Objective-C随机位置无法正常工作(SpriteKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我做了一个randomNumber类(用于练习和),用于计算对象在屏幕上的随机位置,并且可以正常工作,只是有时它会将子画面置于屏幕之外.x和y坐标较小屏幕的高度和宽度.但它不会在屏幕上显示.
整个程序基本上只是在屏幕内随机放置一个对象的实例.

Well I made a randomNumber Class, (for practice and) for calculating a random position on my screen for an object and it works properly, except for it sometimes puts the sprite out of the screen.The x and y coordinates are smaller the screen height and width. but it doesn't show it on the screen.
The whole program is just basically randomly placing instances of an object inside the screen.

randomNumber.h

randomNumber.h

#import <Foundation/Foundation.h>
#import <SpriteKit/SpriteKit.h>

@interface randomNumber : NSObject

-(int)randNumX:(int) max :(SKSpriteNode *) sprite;
-(int)randNumY:(int) max :(SKSpriteNode *) sprite;

@end

randomNumber.m

randomNumber.m

#import "randomNumber.h"

@implementation randomNumber

-(int)randNumX:(int) max :(SKSpriteNode *) sprite {
    int _spriteW = sprite.frame.size.width;

    int _random = (arc4random() % (max - _spriteW));
    NSLog(@"The x value is %d", _random);

    return _random;
}

-(int)randNumY:(int) max :(SKSpriteNode *) sprite {
    int _spriteH = sprite.frame.size.height;

    int _random = (arc4random() % (max - _spriteH));
    NSLog(@"The y value is %d", _random);

    return _random;
}

@end

MyScene.m(仅initilazeMole方法)

MyScene.m ( only the initilazeMole method )

-(void) initilazeMole {
    int x = [self.rndNum randNumX:(self.scene.size.width):(self.mole)];
    int y = [self.rndNum randNumY:(self.scene.size.height):(self.mole)]

    self.mole = [SKSpriteNode spriteNodeWithImageNamed:@"spaceship"];
    self.mole.anchorPoint = CGPointMake(0,0);
    self.mole.position = CGPointMake(x,y);

    SKAction *pulseRed = [SKAction sequence:@[
                                              [SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:1.0 duration:0.5],
                                              [SKAction waitForDuration:0.1],
                                              [SKAction colorizeWithColorBlendFactor:0.0 duration:1.0]]];
    [self.mole runAction: pulseRed];

    NSLog(@"mole x position: %f", self.mole.position.x);
    NSLog(@"mole y position: %f", self.mole.position.y);



    [self addChild:self.mole];
}

我不太明白为什么会将它放置在屏幕之外,因此我生成一个随机数,该随机数最大可以是(屏幕宽度-子画面宽度)和(屏幕高度-子画面高度)
我的项目设置是在横向模式下为3.5英寸的iPhone设置的.知道我的代码在哪里出错了吗?

I don't really understand why does it place it off the screen, hence I generate a random number that can maximally be ( the screen width - sprite width ) and ( the screen height - sprite height )
My project settings are set up for an iphone 3.5 inch in landscape mode. Any idea where did my code go wrong ?

推荐答案

尝试一下:

- (CGPoint) randomPointWithinContainerSize:(CGSize)containerSize forViewSize:(CGSize)size {
    NSLog(@"move");
    CGFloat xRange = containerSize.width - size.width;
    CGFloat yRange = containerSize.height - size.height;

    CGFloat minX = (containerSize.width - xRange) / 2;
    CGFloat minY = (containerSize.height - yRange) / 2;

    int randomX = (arc4random() % (int)floorf(xRange)) + minX;
    int randomY = (arc4random() % (int)floorf(yRange)) + minY;
    return CGPointMake(randomX, randomY);
}

然后替换:

int x = [self.rndNum randNumX:(self.scene.size.width):(self.mole)];
int y = [self.rndNum randNumY:(self.scene.size.height):(self.mole)]

self.mole = [SKSpriteNode spriteNodeWithImageNamed:@"spaceship"];
self.mole.anchorPoint = CGPointMake(0,0);
self.mole.position = CGPointMake(x,y);

使用:

self.mole.position = [self randomPointWithinContainerSize:self.scene.size forViewSize:self.mole.bounds.size];

这篇关于屏幕上的Objective-C随机位置无法正常工作(SpriteKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆