草庐IT

ios - Y 坐标未正确更新

coder 2023-09-27 原文

我正在尝试创建一个横向滚动条,但我无法将对象的 Y 坐标设置为随机值。

我称我的对象为平台。我希望每个平台都出现在不同的 Y 坐标处,并且相信我的方法是正确的做法。但它并不是那么好用。

所有 Y 坐标都输出相同的数字,我不完全确定为什么?我的意思是我在实例化它们时明显添加了间距。

我还注意到的一件事是,如果我不添加计时器并调用移动方法,它们就会出现在正确的位置。所以它可能在调用函数之间。

我发现的另一个问题是,当我再次调用平台时,只有一个平台遵循幻灯片功能要求它做的事情,另外两个平台遵循点,但不对任何其他内容使用react。

非常感谢任何帮助!

//
//  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

最佳答案

y 坐标正在完美更新——在单个点上,您正在检查并在滑动方法中设置所有 C4Shapes 的中心。

在设置中这工作正常,因为你在更改它之前将 C4Shape 的中心设置为 p,但是当你转到 slide 方法中的 For 循环时,你只是记录和更新开始的那个点在最后一个地方,您在设置中更新了它,假设在调用设置和幻灯片之间没有任何反应。 p 是 C4Workspace 类的一个 ivar,因此每个实例都有一个。要解决此问题,我认为您应该将幻灯片中每次出现的 p 更改为 s.center,并删除最后一行。

顺便说一句,你应该认真考虑重命名这些方法中的变量,它们很难遵循——我实际上很困惑为什么 p 是一个 ivar 而不是在设置中声明,这似乎是只放在您需要的地方。

关于ios - Y 坐标未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19739089/

有关ios - Y 坐标未正确更新的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  6. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐