我正在尝试创建一个横向滚动条,但无法将Y
对象的坐标设置为随机值。
我称我的对象为平台。我希望每个平台出现在不同的Y
坐标上,并相信我的方法是正确的做法。但它工作得不是很好。
所有Y
坐标都注销相同的数字,我不完全确定为什么?我的意思是我在实例化它们时显然是在添加间距。
我还注意到的一件事是,如果我不添加计时器并调用移动方法,它们确实会出现在正确的位置。所以它可能是调用函数之间的东西。
我发现的另一个问题是,当我再次调用平台时,只有一个平台遵循幻灯片功能要求它执行的操作,其他 2 个平台遵循这些点,但对其他任何内容都没有反应。
任何帮助都非常感谢!
//
// 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