How to use CGSize or CGpoint as values in a method parameter

Yes, very new to Swift. Within a GameScene I'm creating a background and then an SKShapeNode within it.

Everything works.

So now I have a subclass of the SKShapeNode, and it works for initing the SKShapeNode. So my next my goal is to add a method to position, take care of color, etc.

But I just can't figure out how to pass certain items within methods/functions.

For instance this is the code I use within GameScene, that works:

Code Block
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = -1
self.addChild(background)
pegBase = SKShapeNode(rectOf: CGSize(width: self.size.width * 0.8, height: self.size.height * 0.2))

I just can't seem to figure out how to pass the background.size property. Yes, I could break it down to width and height separately and then pass them to my method, but that sounds kludgy.

Would like to do it correctly.

Thank you

Answered by OOPer in 620606022
The usage is far from what you described as like this.

Why don't you use moveNode like this?
Code Block
moveNode(theNode: sNode, myRect: CGSize(width: background.size.width * 0.8, height: background.size.height * 0.2))



Sorry, but it is not clear in which parts you feel difficulty nor what you think kludgy.

Can you show your break it down to width and height version of your code, including how you want to call it?
Which is the method to which you want to pass background.size ?


After the code in the OP, where my subclassed SKShapeNode is created, I want to call a method that would do everything (color, zPos, shape relative to screen back, etc.). A method that looks like this:

Code Block
pegBaseBoard.setupPBB(bColor: SKColor.red,
bStroke: SKColor.red,
backScreenSize: background.position,
zPos: 0,
dir: direction,
rectOf : CGSize(width: background.size.width * 0.8, height: background.size.height * 0.2))


It's that last parameter I can't seem to declare, or call, correctly. I've been sitting in the Playground non-stop and I just can't figure out the proper syntax.

You may have tried this, but as far as I can see, you just need to declare the method like this:
Code Block
func setupPBB(bColor: SKColor,
bStroke: SKColor,
backScreenSize: CGPoint,
zPos: CGFloat,
dir: CGFloat,
rectOf: CGSize) {
//...
}

If this is not what you want, please tell us what is wrong.
Showing code from playground, not my actual code

So yes, I can declare a func/method like this:

Code Block
func moveNode(theNode : SKShapeNode, myRect : CGSize){
print ("hello")
}

This returns no errors. However when I try to call the func/method like this:

Code Block
moveNode(theNode: sNode, myRect: CGRect)

I get the following error:

Cannot convert value of type 'CGRect.Type' to expected argument type 'CGSize'

Now I realize I am passing in a function, and it returns a struct. I just cannot figure out how to use it in the func/method call

Accepted Answer
The usage is far from what you described as like this.

Why don't you use moveNode like this?
Code Block
moveNode(theNode: sNode, myRect: CGSize(width: background.size.width * 0.8, height: background.size.height * 0.2))



I sincerely don't know what to say. I tried

Code Block
moveNode(theNode: sNode, myRect: CGSize(width: background.size.width * 0.8, height: background.size.height * 0.2))


not this line exactly, but this syntax, many times in the beginning, so many ways. And I kept getting an error similar to

Cannot convert type CGRect.

Over and over, I tried changing so many things and it would not work.

I'm new to Swift, and haven't programmed in over a decade, but I'm not an *****.

So know when I tried it again, as you suggested. It works. I just don't know what happened, or how to say sorry for wasting everyone's time.

Thanks


How to use CGSize or CGpoint as values in a method parameter
 
 
Q