gpt4 book ai didi

swift - Joystick+Firebutton 不能一起工作

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:38 27 4
gpt4 key购买 nike

我正在制作一个应用程序,您可以在其中使用操纵杆控制飞船的运动,并使用射击按钮发射子弹。这两个功能单独使用效果很好,但如果我在用操纵杆移动飞船时点击射击按钮,操纵杆就会变得无法控制。所以我需要的是当我点击屏幕上的任意位置时操纵杆正常工作。

import SpriteKit
import GameplayKit

class GameScene: SKScene {

let player=SKSpriteNode(imageNamed: "prez")
let joystickback=SKSpriteNode(imageNamed: "0")
let joystickbutton=SKSpriteNode(imageNamed: "1")
let shootbutton=SKSpriteNode(imageNamed: "1")
var joystickinuse=false
var velocityX : CGFloat=0.0
var velocityY : CGFloat=0.0

override func didMove(to view: SKView) {

let background=SKSpriteNode(imageNamed: "back")
background.size=self.size
background.position=CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition=0
self.addChild(background)
//joystick back
self.joystickback.position=CGPoint(x: (self.view?.frame.width)!*0.1, y: (self.view?.frame.height)!/5)
self.joystickback.zPosition=1
//jostick butt
self.joystickbutton.position=CGPoint(x: (self.view?.frame.width)!*0.1, y: (self.view?.frame.height)!/5)
self.joystickbutton.zPosition=2
//shootingbutton
self.shootbutton.position=CGPoint(x: (self.view?.frame.width)!*0.9, y: (self.view?.frame.height)!/5)
self.shootbutton.zPosition=2
self.shootbutton.setScale(0.4)

self.ship.position=CGPoint(x: (self.view?.frame.width)!/2, y: (self.view?.frame.height)!/2)
self.ship.setScale(0.2)
self.ship.zPosition=3
self.joystickback.setScale(0.5)
self.joystickbutton.setScale(0.5)
self.addChild(shootbutton)
self.addChild(ship)
self.addChild(joystickback)
self.addChild(joystickbutton)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches{
let location=t.location(in: self)

if (joystickbutton.frame.contains(location)){
joystickinuse=true
}
}
}

func touchMoved(touch:UITouch){
let location=touch.location(in: self)
if (joystickinuse){
let vecktor=CGVector(dx: location.x-joystickback.position.x, dy: location.y-joystickback.position.y)
let angle=atan2(vecktor.dy, vecktor.dx)
let DistanceFromCenter=CGFloat(joystickback.frame.size.height/2)
let DistanceX=CGFloat(sin(angle-CGFloat(M_PI)/2)*DistanceFromCenter)
let DistanceY=CGFloat(cos(angle-CGFloat(M_PI)/2)*DistanceFromCenter)
if(joystickback.frame.contains(location)){
joystickbutton.position=location
}
else{
joystickbutton.position=CGPoint(x: joystickback.position.x-DistanceX, y: joystickback.position.y+DistanceY)
}
velocityX=(joystickbutton.position.x-joystickback.position.x)/10
velocityY=(joystickbutton.position.y-joystickback.position.y)/10
}
}

func firebullet(){
let bullet=SKSpriteNode(imageNamed: "rocket")
bullet.position=urhajo.position
bullet.setScale(0.2)
bullet.zPosition=1
self.addChild(bullet)
let movebullet=SKAction.moveTo(x: self.size.width+bullet.size.width, duration: 1)
let deletebullet=SKAction.removeFromParent()
let bulletsequence=SKAction.sequence([movebullet,deletebullet])
bullet.run(bulletsequence)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches{
self.touchMoved(touch: t)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if(joystickinuse){
movementOver()
}
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
if(joystickinuse){
movementOver()
}
}

func movementOver(){
let moveBack=SKAction.move(to: CGPoint(x: joystickback.position.x,y: joystickback.position.y), duration: TimeInterval(floatLiteral:0.1))
moveBack.timingMode = .linear
joystickbutton.run(moveBack)
joystickinuse=false
velocityY=0
velocityX=0
}

override func update(_ currentTime: TimeInterval) {
if(ship.position.x>=(self.view?.frame.width)!+urhajo.frame.width/2){


}
else{ self.ship.position.x+=velocityX

}

self.ship.position.y+=velocityY
}
}

最佳答案

尝试设置这是你的场景代码

func didMove(to view: SKView) {

self.view!.isMultipleTouchEnabled = true
}

关于swift - Joystick+Firebutton 不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193008/

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