-2

i am trying to generate random coordinates for a button so that every time i press it, it moves to a different coordinate (on the page). is this code correct. if not please show how i would fix it. i have tried to run it but it keeps saying that "button" is not found.

This is the code:

buton.center= CGPointMake(int r = arc4random_uniform(150);,int r = arc4random_uniform(150););

Thanks in advance.

  • Have you checked that `button` is defined? It looks like there's a typo in your code (i.e. you have `buton` instead of `button`). Was this introduced when you typed your code here? – APerson Oct 13 '14 at 03:07

2 Answers2

1

You have misspelled the button variable, and you have passed entire variable declarations as arguments. I won't explain in detail what exactly is wrong here as you should understand C to a reasonable extent (if not, read a C tutorial to cover the basics). This code will work, placing the button's centre at a random position between (0, 0) and (150, 150), assuming your button variable is named button:

button.center = CGPointMake(arc4random_uniform(150), arc4random_uniform(150));

Note that this may result in the button being partially offscreen in the top left corner; you should offset the coordinates to compensate for this:

button.center = CGPointMake(button.frame.size.width / 2.0 + arc4random_uniform(150 - button.frame.size.width), button.frame.size.height / 2.0 + arc4random_uniform(150 - button.frame.size.height));

This code will ensure that the button is always entirely within the rectangle (0, 0), (150, 150).

Greg
  • 9,068
  • 6
  • 49
  • 91
  • Thanks, it also says Identifier not found or '(' what does this mean and how do i fix it? – Connor McCluskey Oct 03 '14 at 05:18
  • What part of the line is underlined with this error? – Greg Oct 03 '14 at 05:19
  • the . in between the button and center – Connor McCluskey Oct 03 '14 at 05:24
  • OK i found the problem that was causing identifier not found, but the code that you gave me doesn't actually move the button, if you have any suggestions please don't hesitate to share :) – Connor McCluskey Oct 03 '14 at 12:14
  • @ConnorMcCluskey make sure that the `button` you're using this code with is actually the button that's on the screen, and isn't nil. Also try disabling auto layout. This code works for me with no problems. I don't know what else you're doing to prevent this from working. – Greg Oct 03 '14 at 12:39
  • Ok, just so i can be sure, how would you check that the button is the button on screen? :) – Connor McCluskey Oct 04 '14 at 07:16
  • How is the button added to the screen? Do you initialise it in code and add it to the view, or is it connected to an `IBOutlet` from a storyboard? – Greg Oct 04 '14 at 07:22
  • IBOutlet from the storyboard – Connor McCluskey Oct 04 '14 at 10:11
  • Is the outlet connected? Can you modify the button another way from the code, e.g. `button.alpha = 0.2`? – Greg Oct 04 '14 at 10:12
  • Are you using auto layout in your storyboard? You can disable it in the first tab in the right pane in interface builder. This code should work after that. You'll need to make adjustments if you want it to work with auto layout. – Greg Oct 04 '14 at 11:21
  • Nope still won't work, i will make a new question and link it so u can see my entire code in the .m – Connor McCluskey Oct 04 '14 at 11:49
  • never mind it won't let me ask any more questions :( – Connor McCluskey Oct 04 '14 at 11:50
0

There is a good code example here of randomly moving a button around the view:

How to move button to random position? (Swift)

Be careful with your syntax. Your "(", ";" and "," are not properly placed.

Community
  • 1
  • 1
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53