Comment dessiner une ligne dans Sprite-kit? Par exemple si je veux dessiner une ligne dans cocos2d, je pourrais facilement utiliser ccDrawLine();
Comment dessiner une ligne dans Sprite-kit
Existe-t-il un équivalent dans le kit de sprite?
Comment dessiner une ligne dans Sprite-kit? Par exemple si je veux dessiner une ligne dans cocos2d, je pourrais facilement utiliser ccDrawLine();
Comment dessiner une ligne dans Sprite-kit
Existe-t-il un équivalent dans le kit de sprite?
En utilisant SKShapeNode vous pouvez tracer une ligne ou n'importe quelle forme.
SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 100.0, 100.0);
CGPathAddLineToPoint(pathToDraw, NULL, 50.0, 50.0);
yourline.path = pathToDraw;
[yourline setStrokeColor:[SKColor redColor]];
[self addChild:yourline];
En utilisant SKShapeNode
J'ai été capable de le faire.
// enter code here
SKShapeNode *line = [SKShapeNode node];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50.0, 40.0);
CGPathAddLineToPoint(path, NULL, 120.0, 400.0);
line.path = path;
[line setStrokeColor:[UIColor whiteColor]];
[self addChild:line];
ligne, line1 ???? – Rajneesh071
Si vous ne voulez une ligne, une sorte de la façon dont les gens utilisent UIViews pour les lignes (uniquement), vous pouvez simplement utiliser un SKSpriteNode
SKSpriteNode* line = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(160.0, 2.0)];
[line setPosition:CGPointMake(136.0, 50.0))];
[self addChild:line];
Voici le code équivalent SWIFT:
let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)
CGPathMoveToPoint(pathToDraw, nil, 100.0, 100)
CGPathAddLineToPoint(pathToDraw, nil, 50, 50)
myLine.path = pathToDraw
myLine.strokeColor = SKColor.redColor()
self.addChild(myLine)
Conversion de l'échantillon de code c de l'objectif @ Rajneesh071.
J'ai trouvé cet article en essayant de dessiner une ligne sur chaque mouseDown, de l'exemple xCode/OS X/Game (aka SpriteKit)/Application.
Vous pouvez copier/coller ce code dans GameScene.swift. Il devrait dessiner une ligne sur chaque événement de souris-vers le bas par l'utilisateur. Looks 'etch-a-sketch' style.
import SpriteKit
var lastPoint: CGPoint = CGPoint(x: 0.0, y: 0.0)
var newPoint: CGPoint = CGPoint(x: 100.0, y: 100.0)
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.blackColor()
let myLabel = SKLabelNode(fontNamed:"default")
myLabel.text = "SKSpriteNode Draw Lines";
myLabel.fontSize = 15;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
}
override func mouseDown(theEvent: NSEvent) {
/* Called when a mouse click occurs */
let location = theEvent.locationInNode(self)
newPoint = location
let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)
CGPathMoveToPoint(pathToDraw, nil, lastPoint.x, lastPoint.y)
CGPathAddLineToPoint(pathToDraw, nil, newPoint.x, newPoint.y)
lastPoint = newPoint
myLine.path = pathToDraw
myLine.strokeColor = SKColor.whiteColor()
self.addChild(myLine)
}
}
Pour les débutants, c'est ce que mon projet Xcode ressemble à:
Pour la petite poignéeiOS de gens. Le même code que ci-dessus a été porté à touchBegan de l'exemple iOS/Jeu (alias SpriteKit)/Projet par défaut de l'application.
Mettez ce code dans votre fichier GameScene.swift
import SpriteKit
var lastPoint: CGPoint = CGPoint(x: 0.0, y: 0.0)
var newPoint: CGPoint = CGPoint(x: 100.0, y: 100.0)
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.blackColor()
let myLabel = SKLabelNode(fontNamed:"default")
myLabel.text = "SKSpriteNode Draw Lines";
myLabel.fontSize = 15;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
newPoint = location
let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)
CGPathMoveToPoint(pathToDraw, nil, lastPoint.x, lastPoint.y)
CGPathAddLineToPoint(pathToDraw, nil, newPoint.x, newPoint.y)
lastPoint = newPoint
myLine.path = pathToDraw
myLine.strokeColor = SKColor.whiteColor()
self.addChild(myLine)
}}}
Profitez.
Swift 3 pour la ligne de dessin via SKShapeNode:
// Define start & end point for line
let startPoint = CGPoint.zero
let endPoint = CGPoint.zero
// Create line with SKShapeNode
let line = SKShapeNode()
let path = UIBezierPath()
path.move(to: startPoint)
path.addLine(to: endPoint)
line.path = path.cgPath
line.strokeColor = UIColor.white
line.lineWidth = 2
@Smick quelle correction voulez-vous dans ce cas? son code de travail .. – Rajneesh071
aucune correction du tout, Waruna devrait lui donner une coche comme son correct. – DogCoffee
@Smick - Waruna acceptera copain de réponse quand il veut ... ne vous inquiétez pas .. sa réponse correcte .. :) – Rajneesh071