Ios Y坐标未正确更新
我试图创建一个侧边滚动条,但我无法将对象的Ios Y坐标未正确更新,ios,objective-c,c4,Ios,Objective C,C4,我试图创建一个侧边滚动条,但我无法将对象的Y坐标设置为随机值 我称我的对象为平台。我希望每个平台以不同的Y坐标出现,并相信我的方法是正确的。但是它不太好用 所有的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
//
//C4WorkSpace.m
//游戏
//
//
#导入“C4Workspace.h”
@实现C4WorkSpace{
C4Shape*player;//player
CGP点,移动;//移动平台和玩家的CG点
int speed;//平台的速度
C4Timer*定时器;//定时器
NSMutableArray*平台;//平台阵列
}
-(无效)设置{
速度=5;//速度限制
p=CGPointMake(self.canvas.width,400);//为平台形状创建两个坐标
move=CGPointMake(0,0);//为用户形状生成2个坐标
平台=[NSMutableArray];//平台的数组指针
//生成形状
对于(int i=0;i<3;i++)
{
C4Shape*s=[C4Shape rect:CGRectMake(0400,50[C4Math randomInt:50]);//制作平台
p、 x=self.canvas.width;//x-平台坐标
p、 y+=100;//y-平台坐标
s、 圆心=p;//圆的圆心是p
[platforms addObject:s];//将平台添加到平台阵列
[self.canvas addShape:platforms[i]];//添加它的实例
计时器=[C4Timer automaticTimerWithInterval:1.0f/30目标:self方法:@“幻灯片”重复:是];//从任何帧拍摄的计时器
}
player=[C4Shape椭圆:CGRectMake(0,0,50,50)];//播放器的形状
[self.canvas addSubview:player];//添加播放器的实例
}
//移动平台
-(无效)幻灯片{
//再次调用平台以添加移动
用于(平台中的C4形状*s){
//添加边界
如果(p.xy坐标正在完美地更新——在单点上,您正在检查并在滑动方法中将所有C4形状的中心设置为
在设置中,这很好,因为您在更改之前将C4形状的中心设置为p,但是当您在幻灯片方法中转到For循环时,您只是记录并更新该点,该点从您在设置中更新它的最后一个位置开始,假设在调用设置和幻灯片之间没有任何变化。p是C4Wo的ivar为了解决这个问题,我认为应该将幻灯片中出现的每一个p都改为s.center,并去掉最后一行
顺便说一句,你应该认真考虑在这些方法中重命名变量,它们很难理解——我实际上很困惑为什么p是一个ivar,而不是在设置中声明,这似乎是你唯一需要它的地方。Ben关于只更新p
变量的回答是正确的。你想要什么o是检查每个单独形状的中心点并进行操作
错误的原因是以下逻辑:
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
}
因为is将所有形状的中心点设置为同一点。但是,只有最后一点起作用
您的方法应如下所示:
-(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;
}
}
-(无效)幻灯片{
//再次调用平台以添加移动
用于(C4Shape*平台中的currentShape){
CGPoint currentCenterPoint=currentShape.center;
如果(currentCenterPoint.x很抱歉迟了回复!非常感谢您的解释!非常有意义:DHey Travis,非常感谢您提供了非常详细的答案。我同意将其添加到自己的类中会更好,但我不完全理解它是如何实现的。我将很快发布我为其编写的代码!它主要只是使用了“@synthesis”@property’这把我搞砸了。它的属性很容易使用,但要真正理解它们需要一些阅读,所以请看:…p.s.大多数情况下你不应该@synthesis啊,我很难完全理解它。Adam教了我们一点,所以我把他的代码拆开,试着保留我需要的并修改了一点。我上传了问题:D
-(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;
}
}