I want to remove a node generated by this function by touching it.
func cuadrado(){
var cuadradoRojo = SKSpriteNode(imageNamed: "cuadradoRojo")
cuadradoRojo.physicsBody = SKPhysicsBody(circleOfRadius: cuadradoRojo.size.width)
cuadradoRojo.physicsBody?.dynamic = true
cuadradoRojo.physicsBody?.categoryBitMask = BodyType.cuadrado.rawValue
cuadradoRojo.physicsBody?.contactTestBitMask = BodyType.colorAzul.rawValue | BodyType.colorRojo.rawValue
cuadradoRojo.physicsBody?.collisionBitMask = 0
var actionArray3:NSMutableArray = NSMutableArray()
if gameOver == false{
let minX = circuloAzul.size.width/2
let maxX = self.frame.size.width - circuloAzul.size.width/2
let rangeX = maxX - minX
let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)
cuadradoRojo.position = CGPointMake(position, self.frame.size.height + cuadradoRojo.size.height)
addChild(cuadradoRojo)
let minDuration = 3
let duration = Int(minDuration)
func touchesBegan(touches: NSSet, withEvent event: UIEvent){
for touch: AnyObject in touches {
let location = (touch as UITouch).locationInNode(self)
if self.nodeAtPoint(location) == self.cuadradoRojo {
cuadradoRojo.removeFromParent()
}
}
}
actionArray3.addObject(SKAction.moveTo(CGPointMake(position, -cuadradoRojo.size.height), duration: NSTimeInterval(duration)))
cuadradoRojo.runAction(SKAction.sequence(actionArray3))
}
It runs perfectly but it doesn't detect the touch, if I put the touches function outside it detects the touch, but the game crashes.
Thanks for your help!
Related
I am trying to get my stationary monster to shoot at my moving player. Both the player position and the monster position are obtaining the right values and this code is written in a function that is called every second. Right now the projectile shows up but doesn't move away from the monster. Is there something else I should be using besides .applyAngularImpulse?
let deltaX = player.position.x - monster.position.x
let deltaY = player.position.y - monster.position.y
let angle = atan2(deltaY, deltaX)
monProjectile.physicsBody?.applyAngularImpulse(angle)
UPDATE
Having looked at the code you provided, I suspect two things are at fault:
1) You are specifying a "projectile" image that I can't see in your project.
2) You are trying to apply angular impulse (i.e. spin) rather than a regular impulse (i.e., direction plus speed).
To fix the first problem add an image for your projectile. To fix the second, consider using applyImpulse() with a CGVector.
Original answer
Off the top of my head, there are a few things that might cause this:
1) How much of an impulse are you applying? Print out the value and see what kind of number you're working with.
2) Does your projectile overlap the monster when it's created? If so, it might be colliding and getting stuck.
3) Is it possible the projectile is colliding with some other node entirely, e.g. a background picture?
You should consider setting showsPhysics to be true for your SKView so you can see what's happening more clearly.
func makeShoot() {
let Shoot:ShootClass = ShootClass.init()
Shoot.physicsBody = SKPhysicsBody(texture: Shoot.texture!,
size: Shoot.texture!.size())
Shoot.position = (self.Shoot?.position)!
Shoot.currentPosition = (self.Shoot?.position)!
Shoot.physicsBody?.isDynamic = true
Shoot.physicsBody?.allowsRotation = false
Shoot.physicsBody?.affectedByGravity = false
Shoot.physicsBody?.friction = 0
Shoot.physicsBody?.restitution = 1
Shoot.physicsBody?.mass = 1
Shoot.physicsBody?.linearDamping = 0.0
Shoot.physicsBody?.angularDamping = 0.0
Shoot.physicsBody?.categoryBitMask = ShootCategory
Shoot.physicsBody?.collisionBitMask = BorderCategory
Shoot.physicsBody?.contactTestBitMask = BorderCategory
PlayingView.addChild(Shoot);
Shoot.physicsBody?.applyImpulse(CGVector(dx: 100, dy: 100))
self.moveNodeToLocation(Shoot: Shoot)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let curTouch = touches.first!
let curPoint = curTouch.location(in: self)
if ((curPoint.x > 103.5 && curPoint.y > 50.0) || (curPoint.x < 840.0 && curPoint.y > 50.0)) {
StartingPoint = touches.first?.location(in: self)
direction?.isHidden = false
direction?.setScale(0.50)
FirstTouchLocater = SKSpriteNode(imageNamed: "ic_Shootz");
FirstTouchLocater.position = curPoint
FirstTouchLocater.alpha = 0.5
self.addChild(FirstTouchLocater);
}
else{
self.direction?.isHidden = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let point:CGPoint = (touches.first?.location(in: self))!
if point.y < 40 {
return
}
if !(isTouch!) {
isTouch = true
}
let dy:CGFloat = StartingPoint!.y - point.y
let size:CGFloat = dy*10/self.frame.height
if size < 2 && size > 0.50 {
direction?.setScale(size)
}
print("size ======> \(size)")
let curTouch = touches.first!
let curPoint = curTouch.location(in: self)
if (curPoint.x <= ((StartingPoint?.x)! + 20.0)) && ((curPoint.x + 20.0) >= (StartingPoint?.x)!) && (curPoint.y <= ((StartingPoint?.y)! + 20.0)) && ((curPoint.y + 20.0) >= (StartingPoint?.y)!){
self.direction?.isHidden = true
FirstTouchLocater?.isHidden = true
}
else if ((curPoint.x > 103.5 && curPoint.y > 50.0) || (curPoint.x < 840.0 && curPoint.y > 50.0)) {
let deltaX = (self.direction?.position.x)! - curPoint.x
let deltaY = (self.direction?.position.y)! - curPoint.y
let angle = atan2(deltaY, deltaX)
let DegreesToRadians = CGFloat.pi / 180
self.direction?.zRotation = angle + 90 * DegreesToRadians
self.direction?.isHidden = false
FirstTouchLocater?.isHidden = false
}
else{
self.direction?.isHidden = true
FirstTouchLocater?.isHidden = true
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if direction?.isHidden == false {
FirstTouchLocater.removeFromParent()
direction?.isHidden = true
direction?.setScale(0.1)
if timeThrow == nil && isTouch!
{
isTouch = false
counterY = 0;
let touch = touches.first
let touchLocation = touch?.location(in: self)
lastTouch = touchLocation
lastTouch1 = touchLocation
ShootThrow = 2
timeThrow = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.loadScreen), userInfo: nil, repeats: true)
}
}
}
func moveNodeToLocation(Shoot:SKSpriteNode) {
let dx = (lastTouch?.x)! - Shoot.position.x
let dy = (lastTouch?.y)! - Shoot.position.y
let speed1:CGFloat = 424
let hypo = hypot(dx, dy)
let newX = (speed1 * dx) / hypo
let newY = (speed1 * dy) / hypo
Shoot.physicsBody?.velocity = CGVector(dx:newX, dy: newY)
}
}
On a project in Xcode 7 I have a few SKSpriteNodes that move back and forth on the screen and another one, called user, that is meant to jump from sprite to sprite, progressively up the screen. However, when user lands on one of the moving sprites the moving sprite just slides right out from under it and user falls back down. I thought that this meant that I needed to increase the friction property on the nodes so that user would "stick" to the nodes, but this just makes it bounce on the other nodes. My problem is that the nodes moving back and forth seem to "slippery," and user just doesn't stay on them.
Here's my code:
My class for user:
class UserNode: SKSpriteNode
{
class func newNode(position position: CGPoint) -> UserNode
{
let position = position
let sprite = UserNode(imageNamed: "userImage")
sprite.position = position
sprite.size = CGSize(width: sprite.size.width * 2, height: sprite.size.height * 2)
sprite.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "userImage"), size: sprite.size)
sprite.physicsBody?.affectedByGravity = true
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.friction = 0.2
return sprite
}
}
and for moving user (the methods in my gamescene)
let scale: CGFloat = 2.0
let damping: CGFloat = 0.98
var point = CGPoint?()
func moveNodeToPoint(sprite: SKSpriteNode, point: CGPoint)
{
let dx = (point.x - sprite.position.x) * scale
let dy = (point.y - sprite.position.y) * scale
sprite.physicsBody?.velocity = CGVectorMake(dx, dy)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
/* Called when a touch begins */
for _: AnyObject in touches
{
if !welcomeNode.hidden
{
let fadeAway = SKAction.fadeOutWithDuration(0.3)
welcomeNode.runAction(fadeAway)
directionsNode.runAction(fadeAway)
touchStartNode.runAction(fadeAway)
welcomeNode.hidden = true
directionsNode.hidden = true
touchStartNode.hidden = true
}
//////////
point = CGPointMake(self.frame.midX, user.position.y + 300)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
point = nil
}
override func update(currentTime: CFTimeInterval)
{
/* Called before each frame is rendered */
if (point != nil)
{
moveNodeToPoint(user, point: point!)
}
else
{
let dx = user.physicsBody!.velocity.dx * damping
let dy = user.physicsBody!.velocity.dy * damping
user.physicsBody?.velocity = CGVectorMake(dx, dy)
}
}
and for moving the platforms:
let screenSize = UIScreen.mainScreen().bounds
let width = screenSize.size.width * 2
let firstAction = SKAction.moveBy(CGVector(dx: width, dy: 0), duration: 2)
let secondAction = SKAction.moveBy(CGVector(dx: -width, dy: 0), duration: 2)
let actions = [firstAction, secondAction]
let barAction = SKAction.sequence(actions)
let mainBarAction = SKAction.repeatActionForever(barAction)
platform.runAction(mainBarAction)
so I am making this game in Swift and I have included the code of what I have done on the 'GameScene.swift' file.
I have 4 different objects that will need to be recreated at run time after every 0.2 seconds (don't even know how to do that :/), however using the code I currently have, if I try to move the 'coin20', 'coin100', or 'coin50' image, they don't move, only 'coin10' gets moved to their location and is far too sensitive so it just moves around like crazy whilst the other images are static.
I hope this question is specific enough since my past questions weren't well received by answerers, if not please let me know how to improve, i'm just a beginner in programming.
Thank you
Code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var coin10 = SKSpriteNode(imageNamed: "10S.png")
var coin100 = SKSpriteNode(imageNamed: "100S.png")
var coin50 = SKSpriteNode(imageNamed: "50S.png")
var coin20 = SKSpriteNode(imageNamed: "20S.png")
var wall1 = SKSpriteNode(imageNamed: "Wall1.png")
var wall2 = SKSpriteNode(imageNamed: "Wall2.png")
var bar = SKSpriteNode(imageNamed: "Bar.png")
var scorelabel = SKLabelNode()
var score = 0
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
enum ColliderType:UInt32 {
case coin = 1
case wall = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Adding coin10
coin10.position = CGPointMake(self.size.width / 2, self.size.height / 5)
coin10.physicsBody = SKPhysicsBody(circleOfRadius: coin10.size.width/2)
coin10.physicsBody!.affectedByGravity = false
coin10.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin10.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.dynamic = true
self.addChild(coin10)
//Adding coin100
coin100.position = CGPointMake(self.size.width / 1.7, self.size.height / 5.1)
coin100.physicsBody = SKPhysicsBody(circleOfRadius: coin100.size.width/2)
coin100.physicsBody!.affectedByGravity = false
coin100.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin100.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.dynamic = true
self.addChild(coin100)
//Adding coin50
coin50.position = CGPointMake(self.size.width / 2.2, self.size.height / 4.9)
coin50.physicsBody = SKPhysicsBody(circleOfRadius: coin50.size.width/2)
coin50.physicsBody!.affectedByGravity = false
coin50.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin50.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin50)
//Adding coin20
coin20.position = CGPointMake(self.size.width / 2.4, self.size.height / 5)
coin20.physicsBody = SKPhysicsBody(circleOfRadius: coin20.size.width/2)
coin20.physicsBody!.affectedByGravity = false
coin20.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin20.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin20.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin20)
//Adding wall1
wall1.position = CGPointMake(self.size.width / 1.72, self.size.height / 1.07)
wall1.physicsBody = SKPhysicsBody(rectangleOfSize: wall1.frame.size )
wall1.physicsBody!.affectedByGravity = false
wall1.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall1.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.dynamic = false
self.addChild(wall1)
//Adding wall2
wall2.position = CGPointMake(self.size.width / 2.5, self.size.height / 1.07)
wall2.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size )
wall2.physicsBody!.affectedByGravity = false
wall2.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall2.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.dynamic = false
self.addChild(wall2)
//Adding bar
bar.position = CGPointMake(self.size.width / 2, self.size.height / 1)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size)
bar.physicsBody!.affectedByGravity = false
bar.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
bar.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
bar.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
bar.physicsBody!.dynamic = false
self.addChild(bar)
//Adding physics world properties
self.physicsWorld.contactDelegate = self
var scenebody = SKPhysicsBody(edgeLoopFromRect: self.frame)
scenebody.friction = 0
self.physicsBody = scenebody
self.physicsWorld.gravity = CGVectorMake(0, -0.3)
//Scoreboard
scorelabel = SKLabelNode(text: "0")
scorelabel.position.y = (self.size.height/2)
scorelabel.position.x = (self.size.height/2.3)
addChild(scorelabel)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
touching = true
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
}
else if coin100.containsPoint(location){
touchPoint = location
}
else if coin50.containsPoint(location){
touchPoint = location
}
else if coin20.containsPoint(location){
touchPoint = location
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-coin10.position.x, dy: touchPoint.y-coin10.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
coin10.physicsBody!.velocity = velocity
}
}
}
First, add this instance variable
var touchedCoin:SKSpriteNode?
Then, in touchesBegan, set touchedCoin to the coin that was touched:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location) {
touchPoint = location
touching = true
touchedCoin = coin10
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin100
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin20
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin50
}
}
}
Lastly, move the coin that was touched
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-touchedCoin!.position.x, dy: touchPoint.y-touchedCoin!.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
touchedCoin!.physicsBody!.velocity = velocity
}
}
I have 2 nodes, that I want to grab with 2 fingers asynchronously on my iPad. That's code for moving:
var ninjaMoving = false
var monsterMoving = false
var loc = CGPoint(x: 0.0, y: 0.0)
var prevLoc = CGPoint(x: 0.0, y: 0.0)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if !self.nodesAtPoint(location).isEmpty && (self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 || self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3) {
if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 {
ninjaMoving = true
}
else if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3 {
monsterMoving = true
}
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
loc = touch.locationInNode(self)
prevLoc = touch.previousLocationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
let location = touch.locationInNode(self)
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 1 {
ninjaMoving = false
}
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 3 {
monsterMoving = false
}
}
}
override func update(currentTime: CFTimeInterval) {
if ninjaMoving {
var x = playerNinja.position.x + (loc.x - prevLoc.x)
var y = playerNinja.position.y + (loc.y - prevLoc.y)
x = max(x, playerNinja.size.width / 2)
x = min(x, size.width / 2 - playerNinja.size.width)
y = max(y, playerNinja.size.height / 2)
y = min(y, size.height - playerNinja.size.height / 2)
playerNinja.position = CGPointMake(x, y)
}
if monsterMoving {
var x = playerMonster.position.x + (loc.x - prevLoc.x)
var y = playerMonster.position.y + (loc.y - prevLoc.y)
x = max(x, self.size.width / 2 + playerMonster.size.width)
x = min(x, size.width - playerMonster.size.width / 2)
y = max(y, playerMonster.size.height / 2)
y = min(y, size.height - playerMonster.size.height / 2)
playerMonster.position = CGPointMake(x, y)
}
}
But I can't move one of sprites to the left, and another to the right at the same time. Is there any way to do that in sprite kit?
Please check the answer for this question: multitouch in spritekit
The idea is that you should track individual touches separately.
I want to generate an infinite amount of nodes that fall from the top of the screen and destroy them by just clicking on them, it is simple but I am having serious problems with it. When the nodes are generated by the loop I can only generate one and it starts moving instead of falling vertically. It also disappears and appears constantly.
Here is my code, hope you can help.
Thank you!!
import SpriteKit
class DestroyScene: SKScene , SKPhysicsContactDelegate{
var velocity:CGFloat = 0
let scoreText = SKLabelNode(fontNamed: "Arial Rounded MT Bold")
var score = 0
var lastYieldTimeInterval:NSTimeInterval = NSTimeInterval()
var lastUpdateTimerInterval:NSTimeInterval = NSTimeInterval()
var gameOver = false
var alien:SKSpriteNode = SKSpriteNode(imageNamed: "circuloAzulArt")
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -velocity)
}
func addAlien(){
var alien:SKSpriteNode = SKSpriteNode(imageNamed: "circuloAzulArt")
alien.name = "alien"
alien.physicsBody = SKPhysicsBody(circleOfRadius: alien.size.width/2)
alien.physicsBody?.dynamic = true
var actionArray:NSMutableArray = NSMutableArray()
var actionArray2:NSMutableArray = NSMutableArray()
alien.removeFromParent()
if gameOver == false{
let minX = alien.size.width/2
let maxX = self.frame.size.width - alien.size.width/2
let rangeX = maxX - minX
let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)
alien.position = CGPointMake(position, self.frame.size.height + alien.size.height)
self.addChild(alien)
let minDuration = 3
let duration = Int(minDuration)
actionArray.addObject(SKAction.moveTo(CGPointMake(position, -alien.size.height), duration: NSTimeInterval(duration)))
actionArray.addObject(SKAction.removeFromParent())
alien.runAction(SKAction.sequence(actionArray))
}
}
func updateWithTimeSinceLastUpdate(timeSinceLastUpdate:CFTimeInterval){
var randomNum = Double(arc4random_uniform(20))
var xTime = ((randomNum / 20) + 0.25)
lastYieldTimeInterval += timeSinceLastUpdate
if (lastYieldTimeInterval > xTime){
lastYieldTimeInterval = 0
randomNum = Double(arc4random_uniform(25))
addAlien()
}
}
override func update(currentTime: CFTimeInterval) {
var timeSinceLastUpdate = currentTime - lastUpdateTimerInterval
lastUpdateTimerInterval = currentTime
if score < 60{
velocity = CGFloat(score*3)
}else{
velocity = CGFloat(210)
}
if (timeSinceLastUpdate > 1){
timeSinceLastUpdate = 1/60
lastUpdateTimerInterval = currentTime
}
updateWithTimeSinceLastUpdate(timeSinceLastUpdate)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.alien{
alien.removeFromParent()
score++
}
}
}
}
Everytime you add an alien you remove the last alien with this line of code:
alien.removeFromParent()
You have to create a new variable node in the addAlien() function and give the node a name
for example:
var alien:SKSpriteNode = SKSpriteNode(imageNamed: "alien")
alien.name = "alien"
Then you can remove the alien in the touchesBegan function by checking the node name
let node = self.nodeAtPoint(location)
if (node.name == "alien") {
}
And for the gravity you should move self.physicsWorld.gravity = CGVectorMake(0, -velocity) to the didMoveToView() function and change the last parameter to a constant