gpt4 book ai didi

ios - 将对象移动到屏幕中央

转载 作者:行者123 更新时间:2023-11-30 13:51:07 25 4
gpt4 key购买 nike

我现在遇到编码问题。我有两堵墙/方 block 移动到屏幕的中心,但是当它们到达中心时,它们一旦到达中心就不会停止。我如何让它们在到达/接触彼此/中心后停止。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
if (playButton.containsPoint(location))
{
playButton.removeFromParent()
title.removeFromParent()
//Wall Timer
wallTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: ("walls"), userInfo: nil, repeats: false)
//Physics World
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
}
else
{}
}
}

func didBeginContact(contact: SKPhysicsContact)
{
if contact.bodyA.node != nil && contact.bodyB.node != nil {
let firstBody = contact.bodyA.node as! SKSpriteNode
let secondBody = contact.bodyB.node as! SKSpriteNode
if ((firstBody.name == "leftWall") && (secondBody.name == "rightWall")) {
collisionWalls(firstBody, rightWall: secondBody)
}
else if ((firstBody.name == "rightWall") && (secondBody.name == "leftWall")) {
collisionWalls(secondBody, rightWall: firstBody)
}
}
}

func collisionWalls(leftWall : SKSpriteNode, rightWall : SKSpriteNode)
{
leftWall.removeAllActions()
rightWall.removeAllActions()
}


func walls() {
let leftWall = SKSpriteNode(imageNamed: "blue background1")
let rightWall = SKSpriteNode(imageNamed: "blue background1")
//Left Wall Code
leftWall.size = CGSizeMake(300, 90)
leftWall.position = CGPoint(x: scene!.frame.width / 6, y: scene!.frame.height / 6)
leftWall.zPosition = 1.0
leftWall.physicsBody = SKPhysicsBody(rectangleOfSize: leftWall.size)
leftWall.physicsBody?.affectedByGravity = false
leftWall.physicsBody?.dynamic = false
leftWall.name = "leftWall"

leftWall.physicsBody?.categoryBitMask = PhysicsCatagory.leftWall
leftWall.physicsBody?.collisionBitMask = PhysicsCatagory.rightWall
leftWall.physicsBody?.contactTestBitMask = PhysicsCatagory.rightWall
leftWall.removeFromParent()
self.addChild(leftWall)

//Right Wall Code
rightWall.size = CGSizeMake(300, 90)
rightWall.position = CGPointMake(self.size.width * 0.87, scene!.frame.height / 6)
rightWall.zPosition = 1.0
rightWall.physicsBody = SKPhysicsBody(rectangleOfSize: rightWall.size)
rightWall.physicsBody?.affectedByGravity = false
rightWall.physicsBody?.dynamic = false
rightWall.name = "rightWall"

rightWall.physicsBody?.categoryBitMask = PhysicsCatagory.rightWall
rightWall.physicsBody?.collisionBitMask = PhysicsCatagory.leftWall
rightWall.physicsBody?.contactTestBitMask = PhysicsCatagory.leftWall
rightWall.removeFromParent()
self.addChild(rightWall)

//Right and Left Wall actions
let moveLeft = SKAction.moveToX(scene!.frame.width * 1.35, duration: 5.0)
let moveRight = SKAction.moveToX(self.size.width * -0.59, duration: 5.0)
leftWall.runAction(SKAction.sequence([moveLeft]))
rightWall.runAction(SKAction.sequence([moveRight]))
}

最佳答案

SKAction 没有停止在屏幕中间。试试这个:

let moveLeft = SKAction.moveToX(scene!.frame.width / 2, duration: 5.0)
let moveRight = SKAction.moveToX(scene!.frame.width / 2, duration: 5.0)

关于ios - 将对象移动到屏幕中央,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com