我想创建一个侧面卷轴,我无法将我的对象的Y
坐标设置为随机值。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
嗨特拉维斯,非常感谢你提供了非常精美的答案。我同意在自己的课堂上加入它会更好,但我不会100%地理解如何去做。我会尽快发布我为之做的代码!这主要是使用'@synthesis''@property',这让我感觉很不舒服。 – tailedmouse
其易于使用的属性,但要真正了解他们需要一些阅读,所以看看:http://www.c4ios.com/tutorials/properties .. p.s.在大多数情况下,你不应该使用@synthesize –
是的,我无法完全理解它。亚当教会了我们这一点,所以我拆开了他的代码,并尝试保留我需要的并修改了一下。我上传了这个问题:D – tailedmouse