0

Hi I'm trying to follow a tutorial on Ray Wenderlich site [http://www.raywenderlich.com/76740/make-game-like-space-invaders-sprite-kit-and-swift-tutorial-part-1][1] so I'm going thru the functions breaking it down so i can get an understanding of how it works I've commented out stuff which i think i understand but this bit has me stumped thanks for looking the for loop whats the var row = 1 at the beginning doing ? I've only ever done for lops like

for Position in 0...9
    {
// do something with Position ten times 
    }

then whats the % in if row %3 mean?

for var row = 1; row <= kInvaderRowCount; row++ // start of loop
    {
        var invaderType: InvaderType // varible of atype etc
        if row % 3 == 0
        {
            invaderType = .AType
        } else if row % 3 == 1

hers the rest of the code

func makeInvaderOfType(invaderType: InvaderType) -> (SKNode) // function passes in a enum of atype,btype,ctype and returns sknode
{
    var invaderColor: SKColor// variable for the colour
    switch(invaderType)// switch statment if we pass in atype we will get red
    {
    case .AType:
        invaderColor = SKColor.redColor()
    case .BType:
        invaderColor = SKColor.greenColor()
    case .CType:
        invaderColor = SKColor.blueColor()
    default:
        invaderColor = SKColor.blueColor()
    }
    let invader = SKSpriteNode(color: invaderColor, size: kInvaderSize)//variable of a skspritenode with color from switch statement  size from vairiabe kinvadersize
    invader.name = kInvaderName // name is invader fron let kinvadername
    return invader //return the spritenode with color size name
}
func setupInvaders()
{

    let baseOrigin = CGPoint(x:size.width/3, y:180) // vairible to hold cgpoint screen size /3 width 180 height
    for var row = 1; row <= kInvaderRowCount; row++ // start of loop
    {
        var invaderType: InvaderType // varible of atype etc
        if row % 3 == 0
        {
            invaderType = .AType
        } else if row % 3 == 1
        {
            invaderType = .BType
        } else
        {
            invaderType = .CType
        }
        let invaderPositionY = CGFloat(row) * (kInvaderSize.height * 2) + baseOrigin.y// varible to hold cgfloat row ? think its the incriment of the for loop  times 16 times 2  = 32 plus 180  first time is 212 then 244 

      /* so if ive got his rightthe sum goes row = 1  kinvadersize.hieght *2 = 32 + baseoringin.y = 180
        1 *  32 +180 = 212
        2 *  32 + 180 = 392  but its 244


     */
        println(row)
        var invaderPosition = CGPoint(x:baseOrigin.x, y:invaderPositionY) // varible to hold cgpoint
        println(invaderPosition.y)
        for var col = 1; col <= kInvaderColCount; col++
        {
            var invader = makeInvaderOfType(invaderType)// varible that runs function and return the spritenode with color size name????
            invader.position = invaderPosition
            addChild(invader)
            invaderPosition = CGPoint(x: invaderPosition.x + kInvaderSize.width + kInvaderGridSpacing.width, y: invaderPositionY)
        }
    }
}
user2164327
  • 283
  • 1
  • 2
  • 13

1 Answers1

0

If I understand your question correctly, here's the answer. Based on this code:

for var row = 1; row <= kInvaderRowCount; row++ // start of loop
    {
        var invaderType: InvaderType // varible of atype etc
        if row % 3 == 0
        {
            invaderType = .AType
        } else if row % 3 == 1

The first line means:

var row = 1: given a new variable, row, with a value of 1

row <= kInvaderRowCount: as long as the variable row is less than or equal to kInvaderRowCount, keep running the for loop

row++: after each time the loop is run, increment (increase) the value of row by 1

As for the "%", that is the modulo operator. It returns the remainder after a division operation on integer values. So if 7 divided by 3 = 2, with a remainder of 1, then

7 / 3 = 2

7 % 3 = 1

The modulus operator results in an integer. While 1 / 3 = 0.33..., 1 % 3 = 1. Because the remainder of 1 divided by 3 is 1.

1 % 3 = 1

2 % 3 = 2

3 % 3 = 0

4 % 3 = 1

5 % 3 = 2

6 % 3 = 0

see also: How Does Modulus Divison Work.

Community
  • 1
  • 1
Andrea
  • 839
  • 8
  • 21
  • hi thanks for the reply i think i get the var row = 1 but doesn't that get reset to 1 every iteration of the loop? so the loop would run indefinitely? – user2164327 Jul 20 '15 at 15:48
  • edit i see if i do it like this row isn't getting reset `code`var row = 1 for ; row <= kInvaderRowCount; row++ // start of loop`code` – user2164327 Jul 20 '15 at 15:54
  • Yup: the for loop structure is such that even though it looks like you are creating a new variable each loop, you aren't: you just start with that localized variable "row" set to 1, but the code "var row = 1" is only run when the for loop is started, not on each iteration of it. – Andrea Jul 20 '15 at 16:07
  • hi thanks this is clearing things up this bit this bit still has me a bit stumped ` let invaderPositionY = CGFloat(row) * (kInvaderSize.height * 2) + baseOrigin.y// varible to hold cgfloat row ? think its the incriment of the for loop times 16 times 2 = 32 plus 180 first time is 212 then 244 /* so if ive got his rightthe sum goes row = 1 kinvadersize.hieght *2 = 32 + baseoringin.y = 180 1 * 32 +180 = 212 2 * 32 + 180 = 392 but its 244 ` – user2164327 Jul 20 '15 at 16:13
  • You've got the order of operations correct: Do all of the multiplication first, then add in the final value. Not sure why you are getting a different value than that - have you checked the 3 values in that formula to make sure they really are what you think they are? – Andrea Jul 20 '15 at 16:29
  • hi thanks i see that now 1 * 32 + 180 then 2 * 32 + 180 so giving a 32 pixel spaceing the modulo you explained I'm still not getting sorted so it goes row = 1 % 3 = 0.333 then 2 % 3 = 0.666 then 3 % 3 = 1 then 4% 3 = 1.33 then 5 % 3 = 1.66 then 6 % 3 = 2 then 7 % 3 = 2.33 then 8 % 3 = 2.66 – user2164327 Jul 20 '15 at 17:02
  • I added a bit more to my answer - hope it helps explain modulo a bit better :) – Andrea Jul 20 '15 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83783/discussion-between-andrea-jessup-and-user2164327). – Andrea Jul 20 '15 at 17:20