I am trying to add multiple instances of my object (bullet) so the player can shoot the bullet and then shoot another bullet. I am using the following code but it is giving me a thread 1 signal sigbart error. Can someone please explain whats wrong with the code? Thanks!
let bulletmove = SKAction.moveTo(y: self.frame.height, duration: 2)
let bulletremove = SKAction.removeFromParent()
addChild(bullett)
bullett.run(SKAction.sequence([bulletmove, bulletremove]))
if you add an SKSprite multiple times it would an error would occur.
What you need to do is create an SKSpritenode to add each time. You can have a function to do create bullets eachtime
func addBullet(){
var bullet = SKSpriteNode(imagenamed: "bullet")
bullet.position = //Give the point of origin as CGPoint. Maybe same as the shooter..
addChild(bullet)
bullet.run(SKAction.moveTo(y: self.frame.height, duration: 2))
}
So each time you call addBullet(),maybe in touches began , you can create new bullet that moves to end of screen without a crash.
So main point is have var bullet = SKSpriteNode(imagenamed: "bullet") for each addChild.
Related
In my macCatalyst app, I try to make a SCNScene where the user should be able to move around (with w, a, s and d) but It doesn't work.
I want
to make the possibility to just move around in the scene first
and then adjust it to the direction that the camera is facing
But I have already failed with the first step.
I tried to set the SCNView's point of view to a new SCNNode and tried to move that around:
//initializing the scene
let scene = SCNScene(named: "SceneKit Scene.scn")!
sceneView.scene = scene
//try to set the point of the scene
let node = SCNNode()
node.position = SCNVector3(x: 10, y: 10, z: 10)
sceneView.pointOfView = node
But it changed nothing.
I have also tried to move the node (that is holding all other nodes of the scene inside around)
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent){
guard let press = presses.first else {return}
guard let key = press.key else {return}
if key.charactersIgnoringModifiers == "w"{
node.position.z += 1
}
}
Which does work better, but it does not feel like going around in the scene, and if I move around the facing of the camera, I move into the wrong direction.
So how do I fix this?
Thanks for helping!
i have an SKSpriteNode named MainTank however when i try to move the tank, it goes left instead of what i did, here's my code
let moveRight = SKAction.moveBy(x: MainTank.position.x + 4, y:0, duration:0.1)
MainTank.run(moveRight)
thanks
The moveBy SKAction moves the sprite by the specified amount, so adding your sprite's current position does not make sense. Not sure why it is moving left but perhaps your sprite is already at a negative position?
To move by a distance of 4 use:
let moveRight = SKAction.moveBy(x:4, y:0, duration:0.1)
Or alternatively use the moveTo SKAction.
Currently working on an application that requires a little bit of animations through an array of images. I've done tons of research online on how to resolve my issue but couldn't run into anything for Swift 4. Below is my code, it shows the first picture in the loop when I run the app but no animation at all. Everything looks fine, I don't see a problem with my code but maybe you guys can help. Appreciate it in advanced!
let atlas = SKTextureAtlas(named: “mypic”)
var TextureArray = [SKTexture]()
var person = SKSpriteNode()
override func didMove(to view: SKView) {
person = SKSpriteNode(imageNamed: "red_1.png")
person.size = CGSize(width: 150, height: 129)
person.position = CGPoint(x: 0, y: 0)
person = SKSpriteNode(imageNamed: atlas.textureNames[0])
for i in 1...atlas.textureNames.count {
let Name = "red_\(i).png"
TextureArray.append(SKTexture(imageNamed: Name))
}
self.addChild(person)
}
override func update(_ currentTime: TimeInterval) {
let myAnimation = SKAction.animate(with: TextureArray, timePerFrame: 0.1)
person.run(SKAction.repeatForever(myAnimation))
}
The animation action is placed in update, which is executed once every frame. So if the game runs at 60 FPS, update gets called 60 times in one second. This means that every second person gets 60 new myAnimation actions that it needs to run.
To fix this, consider placing the animation action somewhere else in your code, e.g. right after adding the person to the scene. Since this is a repeatForever action, the animation will run as you intended until either the action is removed from the node, or the node is removed from the scene.
Hope this helps!
I have run into this strange issue. I am adding an enemy(SKSpriteNode) from inside the GameScene didMove(to view: SKView) using addChild.
The enemy has been positioned to x:100, y: 100 and it appears correctly.
I also have another animation , the completion of which I am adding another enemy at the same location . But the enemy appears at a different location.The completion block is as shown below.
holeExplosion.runHoleExplosionAction {[unowned self] in
//self.addEnemy(enemyCount: 1, hole: holeExplosion)
var modEnemy: ParentEnemy? = nil
modEnemy = Enemy1(imageNamed: "Zombie1Jump1.png", healthPower:30)
print(" \(self.scene?.position.x) \(self.scene?.parent) ")
self.addChild(modEnemy!)
modEnemy!.enemySpeed = self.enemy1Speed
modEnemy!.name = "enemy1"
modEnemy!.position = CGPoint (x: 100 , y: 100)
modEnemy!.zPosition = 2
}
Any help would be appreciated. Thanks.
Your enemy class have physicsBody delegate?? maybe you can see that first because if it has it, you have to search your isDynamic property. You can't have 2 bodies in the same space when the property is equal to true.
I am trying to add two SKSpriteNodes with their respective textures in swift, however the "Blade" does not show up on screen during the simulation. I am having no trouble getting the "Handle" to appear though. So my question is, what is the proper way to add a child of a child of self in swift so that everything works as intended?
var Handle = SKSpriteNode(imageNamed: "Handle.png")
var Blade = SKSpriteNode(imageNamed: "Blade.png")
override func didMoveToView(view: SKView) {
Handle.position = CGPointMake(self.size.width / 2, self.size.height / 14)
Blade.position = CGPointMake(Handle.position.x, Handle.position.y + 124)
self.addChild(Handle)
Handle.addChild(Blade)
}
This should get you close to where you expected it.
Blade.position = CGPointMake(0, 124)
Keep in mind when you add a sprite to another sprite the position is the position "within" that sprite. If you don't change the anchor that starts at the center of its parent. So you were starting in the center of Handle and adding half the width of the scene. Because Handle was already centered in the scene blade was off the scene.
Hopefully that makes sense and is helpful.
Also as lchamp mentioned you should lowerCamelCase
var Blade = SKSpriteNode(imageNamed: "Blade.png")
This will help you in the future identifying Classes vs Variables.