These are chat archives for devslopes/swiftios9

26th
Oct 2015
Mark Price
@spentak
Oct 26 2015 01:42
@TarekSalama I do that frequently
Jack Davis
@jackd942
Oct 26 2015 01:56
Hey @spentak, I know you’re a busy man, but if you get a few secs, could you look at the code here: https://github.com/jackd942/black-isle and tell me if it’s anywhere near the ballpark or if I’m just totally insane:)
Mark Price
@spentak
Oct 26 2015 02:10
looking
ybnormal
@ybnormal
Oct 26 2015 02:11
Hi everyone! Just started Mark's course. Looking forward to it
Mark Price
@spentak
Oct 26 2015 02:13
@ybnormal welcome welcome!
@jackd942 that is super cool! Love the animations
Jack Davis
@jackd942
Oct 26 2015 02:15
Thanks @spentak. It works, but I was wondering about the code. Does it seem somewhat sane? or could it use a bunch of refactoring?
Welcome @ybnormal
Mark Price
@spentak
Oct 26 2015 02:16
To be honest its very well done. Your View Controller manages your outlets and actions and then delegates to the GameController - just what i would od
code is clean
Jack Davis
@jackd942
Oct 26 2015 02:17
ok, thanks. I’m sure there are ways I could clean it up, but I was just trying to make sure I was in the ballpark. I used to do some C# several years back, and it feels real similar to that.
Mark Price
@spentak
Oct 26 2015 02:19
yeah. good OOP design
and MVC
Mo Kash
@MohammedKashem
Oct 26 2015 02:36
Hi guys, I am from the UK. I just joined the course now, its like 2:40am here but I guess I am dedicated and wanting to learn...
Jack Davis
@jackd942
Oct 26 2015 02:37
Nice to have you @MohammedKashem
Mo Kash
@MohammedKashem
Oct 26 2015 02:43
Thank You @jackd942
Hamza Shaikh
@revive14
Oct 26 2015 21:36
Welcome @MohammedKashem bahi 😀😄
jaikamat
@jaikamat
Oct 26 2015 22:45

hi @spentak, I'm trying to add a function to tapperGame make the button move to a random place on the screen each time it's touched. This is my code : """@IBAction func moveButton(button: UIButton!) {

    // find the width and height of the button
    let buttonWidth = coinButton.frame.width
    let buttonHeight = coinButton.frame.height

    // find the width and height of the superview
    let viewWidth = coinButton.superview!.bounds.width
    let viewHeight = coinButton.superview!.bounds.width

    //find the corrdinates of the button onscreen
    let xwidth = viewWidth - buttonWidth
    let ywidth = viewHeight - buttonHeight

    //generate a random set of new x and y coordinates
    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
    let yoffset = CGFloat(arc4random_uniform(UInt32(ywidth)))

    coinButton.center.x = xoffset + buttonWidth/2
    coinButton.center.y = yoffset + buttonWidth/2
}

"""

Sorry if I didn't type that right. But even though it's linked to the button it doesn't move. Not sure if that's because auto layout is enabled, and if you discuss getting around that in future lectures
My code could be completely wrong too, of that might be too advanced for me at this point. Some of this code was chopped together from my research on stackoverflow
Mark Price
@spentak
Oct 26 2015 22:47
Yeah constraints can screw movement up. You might need to set constraints programmatically (fairly complex) every time the button is moved. Your thoughts on the code itself are not bad. Could possibly work fine
Or remove constraints alltogether from the coin button view and then move it around with code like you have here (probably the easiest option)
jaikamat
@jaikamat
Oct 26 2015 22:49
@spentak Thanks, I'll try that option and see if it works! Very appreciated
@spentak so, I removed all constraints from the coinButton, but still no dice. Any thoughts? Can you take a look at my code?
Mark Price
@spentak
Oct 26 2015 22:54
well have you made sure this function is getting called?
also have you made sure that coinButton is an IBOutlet that is connected to your storyboard?
oh
also
dont set x and y individually
do coinButton.center = CGPointMake(xoffset + buttonWidth / 2, yoffset + buttonWidth / 2)
jaikamat
@jaikamat
Oct 26 2015 22:57
Fixed the code with your suggestion. coinButton is set to an IBOutlet. Could it be that there are two separate IBActions both pointing to one IBOutlet? Will that prevent a function from being called?
Mark Price
@spentak
Oct 26 2015 22:57
well an ibaction is called when the user does something on the view (like tap) or if you call it manually
you just need to make sure that your function gets called at the right time
put a breakpoint in that function or do a print("Did I get here")
jaikamat
@jaikamat
Oct 26 2015 22:59
Didn't even realize you could print and view the output while simulating, now I feel like a real n00b. So the function does get called. Still no movement though
Mark Price
@spentak
Oct 26 2015 23:02
at the bottom of the function try doing this and tell me what happens coinButton.frame = CGRectMake(0,0, coinButton.frame.size.width, coinButton.frame.size.height)
jaikamat
@jaikamat
Oct 26 2015 23:03
nothing
Mark Price
@spentak
Oct 26 2015 23:04
try this at the bottom and tell me print(coinButton.debugDescription) or just print(coinButton)
lets see what the console has to say
jaikamat
@jaikamat
Oct 26 2015 23:05

console prints

<UIButton: 0x7f8fa9d23e80; frame = (0 0; 244 268); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7f8fa9d243e0>>

Mark Price
@spentak
Oct 26 2015 23:05
ok so its definitely there - alive and kicking
are you perhaps moving the wrong button?
jaikamat
@jaikamat
Oct 26 2015 23:06
let me try reconnecting my IBActions and outlets, see if it does anything?
Mark Price
@spentak
Oct 26 2015 23:06
well
maybe you have crosswired your buttons and you are expecting it to move but its moving another
for instance, what happens when you do (at the bottom of the function) coinButton.hidden = true does it dissapear?
jaikamat
@jaikamat
Oct 26 2015 23:11
so I reconnected everything. 'coinButton.hidden = true' works! So I know the function is definitely calling on the IBOutlet
must be something wrong with the actual code then
all the constraints have definitely been deleted too
Apologies @spentak, I feel like this is a stupid problem
jaikamat
@jaikamat
Oct 26 2015 23:25
if you get the chance, feel free to look at my code. I'll try to keep working the problem!
https://github.com/jaikamat/tapperGame
Thanks so much for your help, I already learned more than I thought possible