在棋盘精灵套件周围移动几个球 Xcode
moving several balls around a board sprite kit Xcode
我有一个小应用程序,我有几个精灵 "balls" 使用 MotionManager.gravity 在棋盘上移动。
我有代码,当任何一个球遇到边缘时停止在边缘。同样,如果任何一个球到达角落,它就会停在角落里。我还编写了代码,其中如果两个或三个球在任何一个边缘上并相互接触(它们已打开碰撞检测),它们就会停止并保持与另一个球的相对位置。
这是我的代码:
//这会分配 motionManager 并设置重力值。
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 0.005f;
self.motionQueue = [[NSOperationQueue alloc] init];
self.motionQueue.name = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".motion"];
self.updatePosition = NO;
[self.motionManager startDeviceMotionUpdatesToQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
@synchronized(self) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
{
_numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.x / 15.0 *200]];
_numberStringy = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.y / 15.0 *200]];
n = [_numberString intValue];
y = [_numberStringy intValue];
}
self.gravity = motion.gravity;
self.updatePosition = YES;
}
}];
[self startDisplayLink];
//这显示了我如何在边缘停止球
- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
@synchronized(self) {
if (!self.updatePosition)
return;
self.ball.position = CGPointMake(self.ball.position.x + (n*.99), self.ball.position.y + (y*.99));
self.ball2.position = CGPointMake(self.ball2.position.x + (n*.98), self.ball2.position.y +(y*.98));
self.ball3.position = CGPointMake(self.ball3.position.x + n, self.ball3.position.y + y);
if (ball.position.x <=340) {
ball.position = CGPointMake(340, ball.position.y);
}
if (ball.position.x >=684) {
ball.position = CGPointMake(684, ball.position.y);
}
if (ball.position.y <=148) {
ball.position = CGPointMake( ball.position.x,148);
}
if (ball.position.y >=620) {
ball.position = CGPointMake(ball.position.x,620);
}
//这让它停在角落里
if ( ball.position.x >=684 && ball.position.y <=148 ) {
ball.position = CGPointMake(684, 148);
}
if ( ball.position.x >=684 && ball.position.y >=620 ) {
ball.position = CGPointMake(684, 620);
}
if ( ball.position.x <=340 && ball.position.y <=148 ) {
ball.position = CGPointMake(340, 148);
}
if ( ball.position.x <=340 && ball.position.y >=620 ) {
ball.position = CGPointMake(340, 620);
}
//这显示了我是如何设置的,如果它们在一侧,ball1 在角落,ball2 在下一个,ball3 在下一个,它们保持相对位置。我想如果我设置了角球,其余的都打开了碰撞检测,它们就会停下来。他们这样做了,但是他们将角落里的 ball1 推离了它的位置。所以我写了代码
if (ball2.position.y == ball3.position.y && ball.position.y == ball2.position.y && ball.position.x < ball2.position.x && ball2.position.x < ball3.position.x ) {
int s = (self.ball3.position.x - self.ball2.position.x);
int t = ((self.ball2.position.y - self.ball3.position.y));
k = t + s;
int f =(self.ball2.position.x - self.ball.position.x);
int g = ((self.ball2.position.y - self.ball.position.y));
j = f+g;
int h =(self.ball3.position.x - self.ball.position.x);
int i = ((self.ball3.position.y - self.ball.position.y));
l = h + i;
NSLog(@"ball2centerx:%f",ball2.position.x);
NSLog(@"ball2centery:%f",ball2.position.y);
NSLog(@"ballcenterx:%f",ball.position.x);
NSLog(@"ballcentery:%f",ball.position.y);
NSLog(@"ball3centerx:%f",ball3.position.x);
NSLog(@"ball3centery:%f",ball3.position.y);
CGPoint w = CGPointMake(340, 148);
CGPoint x = CGPointMake(340, 620);
CGPoint v = CGPointMake(684, 148);
CGPoint z = CGPointMake(684, 620);
CGPoint m = CGPointMake(370, 148);
CGPoint q = CGPointMake(370, 620);
CGPoint o = CGPointMake(654, 148);
CGPoint p = CGPointMake(654, 620);
if (CGPointEqualToPoint(ball.position,w) ) {
if (f < 30) {
ball2.position = CGPointMake(370, 148);
}
}
if (CGPointEqualToPoint(ball.position,x)) {
if (f<30) {
ball2.position = CGPointMake(370, 620);
}
}
if (CGPointEqualToPoint(ball3.position,v)) {
if (s <30){
ball2.position = CGPointMake(654, 148);
}
}
if (CGPointEqualToPoint(ball3.position,z)) {
if (s <30){
ball2.position = CGPointMake(654, 620);
}
}
if (CGPointEqualToPoint(ball2.position,m)) {
if (s <30){
ball3.position = CGPointMake(400, 148);
}
}
if (CGPointEqualToPoint(ball2.position,o)) {
if (f<30) {
ball.position = CGPointMake(624, 148);
}
}
if (CGPointEqualToPoint(ball2.position,q)) {
if (s <30){
ball3.position = CGPointMake(400, 620);
}
}
if (CGPointEqualToPoint(ball2.position,p)) {
if (f<30) {
ball.position = CGPointMake(624, 620);
}
}
}
问题是,随着我添加更多球,代码将变得极其复杂。
有没有一种方法可以这样写,如果一个球(任何球)在角落里而另一个球在第二个位置,它会留在那儿,依此类推。有点像所有球配置的通用代码。而不是为每个配置编写它?
如果场景中有多个节点,为每个节点创建 属性 会变得乏味且不切实际。一个好的解决方案是创建每个节点并将其添加到数组中。该数组将允许您保留对它的引用。重要的是创建每个节点都有自己唯一的名称。您可以通过创建一个计数器 属性.
来做到这一点
@property (nonatomic) int myCounter;
然后在创建节点时设置节点名称 属性,如下所示:
myCounter++;
myNode.name = [NSString stringWithFormat:@"myNode-%i",myCounter];
完全创建节点后,将其添加到 NSMutableArray,如下所示:
[self.myArray addObject:myNode];
您可以根据需要向数组中添加任意数量的节点。
要枚举数组,您可以这样做:
for (SKSpriteNode *object in myArray) {
// let's look for a node with the name of myNode-2
if([myNode.name isEqualToString:@"myNode-2"]) {
NSLog(@"Found it");
}
}
下面是枚举数组以检查每个节点的位置并根据 IF 语句将其速度设置为零的示例:
for (SKSpriteNode *object in myArray) {
if((object.position.x >= 300) && (object.position.y >= 300)) {
object.physicsBody.velocity = CGVectorMake(0, 0);
}
}
请记住,因为您将数组中的对象存储为 SKSpriteNode 并使用 SKSpriteNode 枚举您的数组,所以您可以访问所有相应的属性,如速度、位置、名称等...
我有一个小应用程序,我有几个精灵 "balls" 使用 MotionManager.gravity 在棋盘上移动。
我有代码,当任何一个球遇到边缘时停止在边缘。同样,如果任何一个球到达角落,它就会停在角落里。我还编写了代码,其中如果两个或三个球在任何一个边缘上并相互接触(它们已打开碰撞检测),它们就会停止并保持与另一个球的相对位置。
这是我的代码:
//这会分配 motionManager 并设置重力值。
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 0.005f;
self.motionQueue = [[NSOperationQueue alloc] init];
self.motionQueue.name = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".motion"];
self.updatePosition = NO;
[self.motionManager startDeviceMotionUpdatesToQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
@synchronized(self) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
{
_numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.x / 15.0 *200]];
_numberStringy = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.y / 15.0 *200]];
n = [_numberString intValue];
y = [_numberStringy intValue];
}
self.gravity = motion.gravity;
self.updatePosition = YES;
}
}];
[self startDisplayLink];
//这显示了我如何在边缘停止球
- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
@synchronized(self) {
if (!self.updatePosition)
return;
self.ball.position = CGPointMake(self.ball.position.x + (n*.99), self.ball.position.y + (y*.99));
self.ball2.position = CGPointMake(self.ball2.position.x + (n*.98), self.ball2.position.y +(y*.98));
self.ball3.position = CGPointMake(self.ball3.position.x + n, self.ball3.position.y + y);
if (ball.position.x <=340) {
ball.position = CGPointMake(340, ball.position.y);
}
if (ball.position.x >=684) {
ball.position = CGPointMake(684, ball.position.y);
}
if (ball.position.y <=148) {
ball.position = CGPointMake( ball.position.x,148);
}
if (ball.position.y >=620) {
ball.position = CGPointMake(ball.position.x,620);
}
//这让它停在角落里
if ( ball.position.x >=684 && ball.position.y <=148 ) {
ball.position = CGPointMake(684, 148);
}
if ( ball.position.x >=684 && ball.position.y >=620 ) {
ball.position = CGPointMake(684, 620);
}
if ( ball.position.x <=340 && ball.position.y <=148 ) {
ball.position = CGPointMake(340, 148);
}
if ( ball.position.x <=340 && ball.position.y >=620 ) {
ball.position = CGPointMake(340, 620);
}
//这显示了我是如何设置的,如果它们在一侧,ball1 在角落,ball2 在下一个,ball3 在下一个,它们保持相对位置。我想如果我设置了角球,其余的都打开了碰撞检测,它们就会停下来。他们这样做了,但是他们将角落里的 ball1 推离了它的位置。所以我写了代码
if (ball2.position.y == ball3.position.y && ball.position.y == ball2.position.y && ball.position.x < ball2.position.x && ball2.position.x < ball3.position.x ) {
int s = (self.ball3.position.x - self.ball2.position.x);
int t = ((self.ball2.position.y - self.ball3.position.y));
k = t + s;
int f =(self.ball2.position.x - self.ball.position.x);
int g = ((self.ball2.position.y - self.ball.position.y));
j = f+g;
int h =(self.ball3.position.x - self.ball.position.x);
int i = ((self.ball3.position.y - self.ball.position.y));
l = h + i;
NSLog(@"ball2centerx:%f",ball2.position.x);
NSLog(@"ball2centery:%f",ball2.position.y);
NSLog(@"ballcenterx:%f",ball.position.x);
NSLog(@"ballcentery:%f",ball.position.y);
NSLog(@"ball3centerx:%f",ball3.position.x);
NSLog(@"ball3centery:%f",ball3.position.y);
CGPoint w = CGPointMake(340, 148);
CGPoint x = CGPointMake(340, 620);
CGPoint v = CGPointMake(684, 148);
CGPoint z = CGPointMake(684, 620);
CGPoint m = CGPointMake(370, 148);
CGPoint q = CGPointMake(370, 620);
CGPoint o = CGPointMake(654, 148);
CGPoint p = CGPointMake(654, 620);
if (CGPointEqualToPoint(ball.position,w) ) {
if (f < 30) {
ball2.position = CGPointMake(370, 148);
}
}
if (CGPointEqualToPoint(ball.position,x)) {
if (f<30) {
ball2.position = CGPointMake(370, 620);
}
}
if (CGPointEqualToPoint(ball3.position,v)) {
if (s <30){
ball2.position = CGPointMake(654, 148);
}
}
if (CGPointEqualToPoint(ball3.position,z)) {
if (s <30){
ball2.position = CGPointMake(654, 620);
}
}
if (CGPointEqualToPoint(ball2.position,m)) {
if (s <30){
ball3.position = CGPointMake(400, 148);
}
}
if (CGPointEqualToPoint(ball2.position,o)) {
if (f<30) {
ball.position = CGPointMake(624, 148);
}
}
if (CGPointEqualToPoint(ball2.position,q)) {
if (s <30){
ball3.position = CGPointMake(400, 620);
}
}
if (CGPointEqualToPoint(ball2.position,p)) {
if (f<30) {
ball.position = CGPointMake(624, 620);
}
}
}
问题是,随着我添加更多球,代码将变得极其复杂。
有没有一种方法可以这样写,如果一个球(任何球)在角落里而另一个球在第二个位置,它会留在那儿,依此类推。有点像所有球配置的通用代码。而不是为每个配置编写它?
如果场景中有多个节点,为每个节点创建 属性 会变得乏味且不切实际。一个好的解决方案是创建每个节点并将其添加到数组中。该数组将允许您保留对它的引用。重要的是创建每个节点都有自己唯一的名称。您可以通过创建一个计数器 属性.
来做到这一点@property (nonatomic) int myCounter;
然后在创建节点时设置节点名称 属性,如下所示:
myCounter++;
myNode.name = [NSString stringWithFormat:@"myNode-%i",myCounter];
完全创建节点后,将其添加到 NSMutableArray,如下所示:
[self.myArray addObject:myNode];
您可以根据需要向数组中添加任意数量的节点。
要枚举数组,您可以这样做:
for (SKSpriteNode *object in myArray) {
// let's look for a node with the name of myNode-2
if([myNode.name isEqualToString:@"myNode-2"]) {
NSLog(@"Found it");
}
}
下面是枚举数组以检查每个节点的位置并根据 IF 语句将其速度设置为零的示例:
for (SKSpriteNode *object in myArray) {
if((object.position.x >= 300) && (object.position.y >= 300)) {
object.physicsBody.velocity = CGVectorMake(0, 0);
}
}
请记住,因为您将数组中的对象存储为 SKSpriteNode 并使用 SKSpriteNode 枚举您的数组,所以您可以访问所有相应的属性,如速度、位置、名称等...