官术网_书友最值得收藏!

Building a SKSpriteNode class

Let's begin by drawing a blue square on the screen. The SKSpriteNode class can draw both texture graphics and solid blocks of color. It is often helpful to prototype your new game ideas with blocks of color before you spend time on artwork. To draw the blue square, add an instance of SKSpriteNode to the game:

Override func didMove(to view: SKView) { 
    // Make the scene position from its lower left 
    // corner, regardless of any other settings: 
self.anchorPoint = .zero 

    // Instantiate a constant, mySprite, instance of SKSpriteNode
    // The SKSpriteNode constructor can set color and size 
    // Note: UIColor is a UIKit class with built-in colorpresets
    // Note: CGSize is a type we use to set node sizes 
let mySprite = SKSpriteNode(color: .blue, size: 
CGSize(width: 50, height: 50)) 

    // Assign our sprite a position in points, relative to its 
    // parent node (in this case, the scene) 
mySprite.position = CGPoint(x: 150, y: 150) 

    // Finally, we need to add our sprite node into the node tree. 
    // Call the SKScene'saddChild function to add the node 
    // Note: In Swift, 'self' is an automatic property 
    // on any type instance, exactly equal to the instance itself 
    // So in this case, it refers to the GameScene instance 
self.addChild(mySprite) 
} 

Go ahead and run the project. You should see a similar small blue square appear in your simulator:

Building a SKSpriteNode class

Tip

Swift allows you to define variables as constants, which can be assigned a value only once. For the best performance, use let to declare constants whenever possible. Declare your variables with var when you need to alter the value later in your code.

主站蜘蛛池模板: 亳州市| 马关县| 若尔盖县| 三都| 新晃| 鄢陵县| 丰台区| 古丈县| 丰镇市| 册亨县| 长葛市| 渭南市| 镇巴县| 喀什市| 苍溪县| 塘沽区| 南昌市| 陕西省| 辉县市| 拉孜县| 英超| 博爱县| 靖西县| 贺兰县| 固镇县| 婺源县| 库车县| 山西省| 瓦房店市| 南岸区| 西安市| 昆明市| 平昌县| 宜州市| 崇明县| 辽阳市| 阿拉善左旗| 界首市| 突泉县| 略阳县| 阿拉善右旗|