我正在尝试创建侧卷轴,但我无法将对象的Y
坐标设置为随机值。
我称我的对象平台。我希望每个平台都出现在不同的Y
坐标处,并且相信我的方法是正确的方法。但它运作得不好。
所有Y
坐标都记录了相同的数字,我不完全确定原因?我的意思是我在实例化时明显添加了间距。
我注意到的一件事是,如果我不添加计时器并调用移动方法,它们会显示在正确的位置。所以它可能是调用函数之间的东西。
我发现的另一个问题是,当我再次调用平台时,只有一个平台遵循幻灯片功能要求它执行的操作,另外两个平台遵循这些点,但不会对其他任何内容做出反应。
任何帮助都非常感谢!
//
// C4WorkSpace.m
// TheGame
//
//
#import "C4Workspace.h"
@implementation C4WorkSpace {
C4Shape *player ; // player
CGPoint p, move; // CG point for moving platforms && Players
int speed; // Speed of the platforms
C4Timer *timer; // Timer
NSMutableArray *platforms; // Platform Array
}
-(void)setup {
speed = 5; // Speed Limit
p = CGPointMake(self.canvas.width, 400); // Making 2 coordinates for the platform shape to follow
move = CGPointMake(0, 0); // Making 2 coordinates for the user shape to follow
platforms = [NSMutableArray array]; // Pointer of Array for platforms
// Generating shapes
for ( int i = 0; i < 3; i++)
{
C4Shape * s = [C4Shape rect:CGRectMake(0, 400, 50, [C4Math randomInt:50])]; // Making the platform
p.x = self.canvas.width; // x - coordinate for the platforms
p.y += 100; // y - coordinate of the platforms
s.center = p; // The Center of the Circle is P
[platforms addObject:s]; // Adding platforms to the platforms array
[self.canvas addShape:platforms[i]]; // Adding an instance of it
timer = [C4Timer automaticTimerWithInterval:1.0f/30 target:self method:@"slide" repeats:YES]; // Timer to shoot it off ever frame
}
player = [C4Shape ellipse:CGRectMake(0, 0, 50, 50)]; // The shape of the player
[self.canvas addSubview:player]; // Adding an instance of the player
}
//Moving the platform
-(void) slide {
//Calling the platforms again to add movement
for (C4Shape *s in platforms){
// Adding boundries
if (p.x <= 0 ) {
p.x = self.canvas.width; // if it's smaller than the width of the cavas auto transport
p.y = [C4Math randomInt:self.canvas.height]; // choose a different y coordinate for each
}
p.x-= speed; // Adding accelaration
C4Log(@"The Y is .%2f", p.y); // Logging the problem
s.center = p; // making the shape follow the point
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *place = [[event allTouches] anyObject]; // Get touches
move = [place locationInView:place.view]; // Gets the location of the current mouse point
player.center = move; // folllowing the move point
[self collisionCheck]; // collision check
}
-(void) collisionCheck {
//currently empty!
}
@end
答案 0 :(得分:3)
y坐标完美更新 - 在您检查的单个点上,并在幻灯片方法中设置所有C4Shapes的中心。
在设置中,这可以正常工作,因为在更改之前将C4Shape的中心设置为p,但是当您在slide方法中转到For循环时,您只是记录并更新那一点,在最后一个地方,您在设置中更新它,假设在调用设置和幻灯片之间没有任何反应。 p是C4Workspace类的ivar,因此每个实例都有一个。为了解决这个问题,我相信你应该将幻灯片中每次出现的p更改为s.center,并删除最后一行。
顺便提一下,你应该认真考虑在这些方法中重命名变量,它们很难遵循 - 我实际上很困惑为什么p是一个ivar并且没有在设置中声明,这似乎是只有你需要它。
答案 1 :(得分:2)
Ben关于仅更新p
变量的答案是正确的。你想要做的是检查每个单独形状的中心点并操纵它。
错误的原因是这样的逻辑:
for(every shape in platforms) {
check to see if a point p is off the screen
if it is, then change its value to a random number
then update the speed of p
set the centerpoint of the current shape to p
}
上面的逻辑是你在这里编写的:
for (C4Shape *s in platforms) {
if (p.x <= 0 ) {
p.x = self.canvas.width;
p.y = [C4Math randomInt:self.canvas.height];
}
p.x-= speed; // Adding accelaration
s.center = p; // making the shape follow the point
}
问题在于:
s.center = p; // making the shape follow the point
因为将形状的所有中心点设置为相同的点。但是,只有最后一点才有所作为。
您的方法应如下所示:
-(void) slide {
//Calling the platforms again to add movement
for (C4Shape *currentShape in platforms) {
CGPoint currentCenterPoint = currentShape.center;
if (currentCenterPoint.x <= 0 ) {
// if it's smaller than the width of the cavas auto transport
currentCenterPoint.x = self.canvas.width;
// choose a different y coordinate for each
currentCenterPoint.y = [C4Math randomInt:self.canvas.height];
}
currentCenterPoint.x-= speed; //Adding accelaration
currentShape.center = currentCenterPoint;
}
}
另外,请注意此方法重命名变量,以便代码更具可读性。这是一个很好的做法,可以帮助记住正在发生的事情,并让其他人更容易阅读您的代码。
注意:你真的很接近能够将这种功能添加到自己的类中,以使自己更简单,工作更好!