Hello, I am interested in creating a risk-style 2D game in spritekit and swift 3
ID: 3780328 • Letter: H
Question
Hello,
I am interested in creating a risk-style 2D game in spritekit and swift 3. I'm looking for implementation suggestions on the game board. My idea right now is simply to draw a bunch of countries in photoshop and then add them to the game at specific CG points as SKSpirteNodes. The problem is that manually positioning 10-20 or 30 countries would be a headache and I am sure there is a better way to do it.
Each country should be selectable by tapping on it and be highlightable on selection. I've looked at Paint Code 2 as a possible solution to this problem, but I feel like it wouldn't be able to draw the countries as seperate sprites appropriately. My only other guess is to use SKShapeNode to achieve the complex borders of countries.
Thanks!
Explanation / Answer
(1) If you want the countries to be sprites, then you should probably try the below:
i. You should make the sprite for each country just big enough to contain them.
ii. You should make a UIBezierPath for each of the country and then save it as a property of that particular country.
iii. You should use containsPoint in the touchesBegan of each of the country to check if touch is inside.
(2) If you don't want to use SpriteKit (for this particular part) then you should:
Draw the entire map with vector graphics using the drawRects (or CAShapeLayers)
Use the same bezier paths for drawing and for touch detection.
(3) If you could use vector graphics in SpriteKit using the same approach as in above (2) point with the SKShapeNode. In this case, this may work.
In either case, You will have to use the amazing app PaintCode to make the country shapes ready. Also, there are other free alternatives that will let you convert your
SVG drawings to Swift bezier paths.
If(yourCountrySKspritenode contains:location)
{
//To change your Country SKspritenode image to another duplicate image but brighter
//execute whatever else you want to add here
}
The below program may also help you:
import SpriteKit
public class BoardGameObject: SKSpriteNode { //Creating a public class here
init( texture: SKTexture?, color: UIColor, size: CGSize, position:CGPoint, name:String ) //texture,color,size,point are declared here
{
objectSize = size;
objectName = name;
objectSprite = texture;
// superclass will be called here
super.init(texture: texture, color: color, size: size);
self.position = position;
}
convenience init(_ _x:CGFloat, _ _y:CGFloat, _ _object:String )// This is the Default initializer
{
let texture = SKTexture(imageNamed: _object);
let position = CGPoint(x:_x, y:_y);
self.init( texture: texture,color: UIColor(),size: texture.size(), position: position, name: _object);
}
//This will Overload the initializer with size as extra argument
convenience init(_ _x:CGFloat, _ _y:CGFloat, _ _size:Int, _ _object:String)
{
let texture = SKTexture(imageNamed: _object); //This will be the size for the SKSpriteNode.
let position = CGPoint(x:_x, y:_y);
self.init( texture: texture, color: UIColor(), size: CGSize(width: _size, height: _size),position: position, name: _object);
}
To instantiate a country from BoardGameObject,
let country= Countries(100, 100, "USA") // This is how countries can be added.
addChild(country)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.