Bonjour,
Je me permet de venir vers vous car j'ai jusque la eu aucune réponse à mon problème... et cela m'embête depuis 2 jours pour avancer![]()
En effet, je suis nouveau dans le langage Swift, et je suis en train de développer une application iOS, un jeu plus précisément de type RUNNER infini.
Je me retrouve bloqué a une condition de saut qui devrait faire en sorte que le personnage ne puisse pas sauter 10000 fois n'importe quand, mais qu'il puisse sauter, et a nouveau sauter une fois qu'il a touché le sol. Pas avant. Cependant, ça marche pas...
Soit mon personnage saute qu'une seule fois, et on dirait qu'il n'arrive pas a reconnaître qu'il a touché le sol donc il ne re-saute pas, soit il saute toujours a l'infini tant que je clique.
Voici les codes sources : (j'ai mis en gras les parties concernées)
Merci d'avance pour votre aide, et votre temps accordé à mon problème.
Cordialement,
O. Deger DEMIRDES
GameplayScene :
import SpriteKit
class GameplayScene: SKScene, SKPhysicsContactDelegate {
var player = Player();
var obstacles = [SKSpriteNode]();
var canJump = true;
var movePlayer = false;
var playerOnObstacle = false;
override func didMove(to view: SKView) {
initialize();
}
override func update(_ currentTime: TimeInterval) {
moveBackgroundsAndGrounds();
if movePlayer {
player.position.x -= 9;
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if canJump {
canJump = false;
player.jump();
}
if playerOnObstacle {
player.jump();
}
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody();
var secondBody = SKPhysicsBody();
if contact.bodyA.node?.name == "Player" {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Ground" {
canJump = true;
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Obstacle" {
movePlayer = false;
playerOnObstacle = false;
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Cactus" {
//kill the player when he touches a Cactus and prompt the replay or menu button
}
func didEndContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody();
var secondBody = SKPhysicsBody();
if contact.bodyA.node?.name == "Player" {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Obstacle" {
if !canJump {
movePlayer = true;
playerOnObstacle = true;
}
}
}
}
func initialize() {
physicsWorld.contactDelegate = self;
createPlayer();
changeBG();
changeGround();
createObstacles();
Timer.scheduledTimer(timeInterval: TimeInterval(randomBetweenNumbers(firstNumber: 2.5, secondNumber: 6)), target: self, selector: #selector(GameplayScene.spawnObstacles), userInfo: nil, repeats: true)
}
func createPlayer() {
player = Player(imageNamed: "Player 1");
player.initialize();
player.position = CGPoint(x: -10, y: 20);
self.addChild(player);
}
func changeBG() {
for i in 0...2 {
let bg = SKSpriteNode(imageNamed: "BG");
bg.name = "BG";
bg.anchorPoint = CGPoint(x: 0.5, y: 0.5);
bg.position = CGPoint(x: CGFloat(i) * bg.size.width, y: 0);
bg.zPosition = 0;
self.addChild(bg);
}
}
func changeGround() {
for i in 0...2 {
let bg = SKSpriteNode(imageNamed: "Ground");
bg.name = "Ground";
bg.anchorPoint = CGPoint(x: 0.5, y: 0.5);
bg.position = CGPoint(x: CGFloat(i) * bg.size.width, y: -(self.frame.size.height / 2));
bg.zPosition = 2;
bg.physicsBody = SKPhysicsBody(rectangleOf: bg.size);
bg.physicsBody?.affectedByGravity = false;
bg.physicsBody?.isDynamic = false;
bg.physicsBody?.categoryBitMask = ColliderType.Ground;
self.addChild(bg);
}
}
func moveBackgroundsAndGrounds() {
enumerateChildNodes(withName: "BG", using: ({
(node, Error) in
let bgNode = node as! SKSpriteNode;
bgNode.position.x -= 2;
if bgNode.position.x < -(self.frame.width) {
bgNode.position.x += self.frame.width * 3;
}
}))
enumerateChildNodes(withName: "Ground", using: ({
(node, Error) in
let bgNode = node as! SKSpriteNode;
bgNode.position.x -= 5;
if bgNode.position.x < -(self.frame.width) {
bgNode.position.x += self.frame.width * 2;
}
}))
}
func createObstacles() {
for i in 0...5 {
let obstacle = SKSpriteNode(imageNamed: "Obstacle \(i)");
if i == 0 {
obstacle.name = "Cactus";
obstacle.setScale(0.4);
} else {
obstacle.name = "Obstacle";
obstacle.setScale(0.5);
}
obstacle.anchorPoint = CGPoint(x: 0.5, y: 0.5);
obstacle.zPosition = 1;
obstacle.physicsBody = SKPhysicsBody(rectangleOf: obstacle.size);
obstacle.physicsBody?.allowsRotation = false;
obstacle.physicsBody?.categoryBitMask = ColliderType.Obstacle;
obstacles.append(obstacle);
}
}
func spawnObstacles() {
let index = Int(arc4random_uniform(UInt32(obstacles.count)));
let obstacle = obstacles[index].copy() as! SKSpriteNode;
obstacle.position = CGPoint(x: self.frame.width + obstacle.size.width, y: 50);
let move = SKAction.moveTo(x: -(self.frame.size.width * 2), duration: TimeInterval(15));
let remove = SKAction.removeFromParent();
let sequence = SKAction.sequence([move, remove]);
obstacle.run(sequence);
self.addChild(obstacle);
}
func randomBetweenNumbers(firstNumber: CGFloat, secondNumber: CGFloat) -> CGFloat {
//arc4random returns a number between 0 to (2**32) -1
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNumber - secondNumber) + min(firstNumber, secondNumber);
}
}
Classe Player :
import SpriteKit
struct ColliderType {
static let Player: UInt32 = 1;
static let Ground: UInt32 = 2;
static let Obstacle: UInt32 = 3;
}
class Player: SKSpriteNode {
func initialize() {
self.name = "Player";
self.zPosition = 2;
self.anchorPoint = CGPoint(x: 0.5, y: 0.5);
self.setScale(0.5);
self.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width - 20, height: self.size.height
));
self.physicsBody?.affectedByGravity = true;
self.physicsBody?.allowsRotation = false;
self.physicsBody?.categoryBitMask = ColliderType.Player;
self.physicsBody?.collisionBitMask = ColliderType.Ground | ColliderType.Obstacle;
self.physicsBody?.contactTestBitMask = ColliderType.Ground | ColliderType.Obstacle;
}
func jump() {
self.physicsBody?.velocity = CGVector(dx: 0, dy: 0);
self.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200));
}
}
Partager