Rotieh Posted January 14, 2015 Share Posted January 14, 2015 Hello. First of all, I would like to apologize about my poor english, which did not allow me to make a decent search (i tried to find another topic with the same query, but they were different in some way, as this one: http://www.html5gamedevs.com/topic/10826-jumping-problem/?hl=jump). I'm really sorry if i'm creating an already answered topic =( THE PROBLEM: Easy for you. I was following this code: http://examples.phaser.io/_site/view_full.html?d=p2%20physics&f=tilemap+gravity.js&t=tilemap%20gravity I can't explain why I couldn't get my player to jump. The code was identic... after many tries I just copy/paste the code from the example. Nothing. Can you help me? It's probably one of the silliest questions you have seen here, so forgive me. THE CODE:function carregaAssets() { //Carregando o cenário game.load.tilemap('map', 'assets/untitled.json', null, Phaser.Tilemap.TILED_JSON); // Carregando a imagem base (tilemap) do mapa: game.load.image('tilesCenario', 'assets/novo2.png'); // Carregando a imagem de fundo - bg.png: game.load.image('fundo', 'assets/bg.jpg'); // Adiciona sprites do jogador: game.load.spritesheet('dude', 'assets/dude.png', 32, 48); }var map;var layerMap1;var layerMap2;var player;var cursors;var jumpButton;var facing = 'left';var jumpTimer = 0;function criaCenario() { // Cria o background: criaFundo(); // Inicia a física no jogo: game.physics.startSystem(Phaser.Physics.P2JS); // Início do processo de mapeamento - Variável recebe o arquivo JSON 'map': map = game.add.tilemap('map'); // Insere o Tilemap (imagem) dentro dos parâmetros JSON: map.addTilesetImage('novo2', 'tilesCenario'); // Cria as layers do cenário com o mesmo nome do TILED: layerMap1 = map.createLayer('camada1'); layerMap2 = map.createLayer('Colisao'); layerMap1.resizeWorld(); layerMap2.resizeWorld(); // Criando colisões no cenário: map.setCollision(79); // Transformando tilemaps de colisão em objetos (CORPO): game.physics.p2.convertTilemap(map, layerMap2); // MUDAR DEPOIS game.physics.p2.restitution = 0.5; game.physics.p2.gravity.y = 300; // Chama função Jogador criaPlayer(); // Configura os botões de movimentação: cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); }function atualizaJogo() { if (cursors.left.isDown) { player.body.moveLeft(200); if (facing != 'left') { player.animations.play('left'); facing = 'left'; } } else if (cursors.right.isDown) { player.body.moveRight(200); if (facing != 'right') { player.animations.play('right'); facing = 'right'; } } else { player.body.velocity.x = 0; if (facing != 'idle') { player.animations.stop(); if (facing == 'left') { player.frame = 0; } else { player.frame = 5; } facing = 'idle'; } } if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump()) { player.body.moveUp(300); jumpTimer = game.time.now + 750; }}//######################FUNÇÕES EXTRAS #############################function criaFundo(){ bg = game.add.tileSprite(0, 0, 800, 520, 'fundo'); bg.fixedToCamera = true;}function criaPlayer(){ player = game.add.sprite(100, 200, 'dude'); player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('turn', [4], 20, true); player.animations.add('right', [5, 6, 7, 8], 10, true); game.physics.p2.enable(player); player.body.fixedRotation = true; // player.body.setMaterial(characterMaterial); game.camera.follow(player);}function checkIfCanJump() { var yAxis = p2.vec2.fromValues(0, 1); var result = false; for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) { var c = game.physics.p2.world.narrowphase.contactEquations[i]; if (c.bodyA === player.body.data || c.bodyB === player.body.data) { var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis if (c.bodyA === player.body.data) d *= -1; if (d > 0.5) result = true; } } return result;}function render() {}Thanks a lot, and sorry again... Link to comment Share on other sites More sharing options...
CtlAltDel Posted January 14, 2015 Share Posted January 14, 2015 Your code is missing the actual phaser game creation. My spanish is not so great so matching those function names to preload/create/update is kinda hard Link to comment Share on other sites More sharing options...
Rotieh Posted January 14, 2015 Author Share Posted January 14, 2015 Oh my god, I'm really embarrassed! Actually I have this piece of code, is on a separate page. Please forgive-me.window.onload = function() { game = new Phaser.Game(800, 520, Phaser.AUTO, 'divGame', { preload: carregaAssets, create: criaCenario, update: atualizaJogo });}Really sorry... =( The game is working, just can't jump =/ Link to comment Share on other sites More sharing options...
zencha Posted January 14, 2015 Share Posted January 14, 2015 I'm not sure how moveUp works... in my games I'm using player.body.velocity.y to do the jump. Link to comment Share on other sites More sharing options...
CtlAltDel Posted January 15, 2015 Share Posted January 15, 2015 It might be an issue with your tilemap and the physics settings in the json or something. Not sure how that works Link to comment Share on other sites More sharing options...
valueerror Posted January 15, 2015 Share Posted January 15, 2015 you need to narrow it down...what happens if you change the jump function to this.. does the player jump? is the jump function even triggered ? try a console.log in it and see if you are even getting there.. if (jumpButton.isDown ){console.log('jump');player.body.moveUp(300);} Link to comment Share on other sites More sharing options...
Rotieh Posted January 16, 2015 Author Share Posted January 16, 2015 Thank you for listening, guys... you need to narrow it down...what happens if you change the jump function to this.. does the player jump? is the jump function even triggered ? try a console.log in it and see if you are even getting there.. if (jumpButton.isDown ){console.log('jump');player.body.moveUp(300);} Yes, I've already tried this.... In this case the player can jump, but even without being touching the ground ... =( console.log? (Oh god, so ashamed...) I googled it , it's just like in the php? --------- I'm not sure how moveUp works... in my games I'm using player.body.velocity.y to do the jump. Yeah... I can think in the algorithm to this... Something like this (please, I'm trying to learn yet, reading about algorithms and trying to figure out what would work here... it will surely be flawed =P)if (jumpButton.isDown){ // I don't know how to code the line below: if (*player is touching the ground*) { if (player.body.velocity.x == 0){ player.animations.play('idleJumpUP') var axisY = player.body.velocity.y; player.body.velocity.y += 750; //750 is just an example; // Now I need some kind of measuring for the Y axis... *When (player.body.velocity.y == axisY + 750){ // No idea... ashamed... =( player.animations.play('idleJumpDOWN'); player.body.velocity.y -= 750; if (player.body.velocity.x == 0){ player.animations.play('idle'); player.animations.stop(); } else if (player.body.velocity.x != 0){ // Or when Left or Right button is Down if (facing == 'left'){ player.frame = 0; } else { player.frame = 5; } player.animations.play('running'); } } } else if (player.body.velocity.x != 0){ player.animations.play('movingJumpUP') var axisY = player.body.velocity.y; player.body.velocity.y += 750; //same example; // Again... i don't know how to measure the Y: *When (player.body.velocity.y == axisY + 750){ // No idea... ashamed... =( player.animations.play('movingJumpDOWN'); player.body.velocity.y -= 750; if (player.body.velocity.x == 0){ player.animations.play('idle'); player.animations.stop(); } else if (player.body.velocity.x != 0){ // Or when Left or Right button is Down if (facing == 'left'){ player.frame = 0; } else { player.frame = 5; } player.animations.play('running'); } } } }} Link to comment Share on other sites More sharing options...
valueerror Posted January 16, 2015 Share Posted January 16, 2015 the function moveUp(speed) does exactly that: velocity.y = -speed; nothing magical... soooo.. if you are able to jump without the additional checks then which check is responsible for it ?? why so quiet about your findings? is it "game.time.now > jumpTimer" ?? does it work without it oris it "checkIfCanJump()" ??? if it's the checkIfCanJump() you need to debug this function further.. i'm using the similar function : (needs a p2 physics body... so start with if ( touchingDown(player.body) ){ }function touchingDown(someone) { var yAxis = p2.vec2.fromValues(0, 1); var result = false; for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) { var c = game.physics.p2.world.narrowphase.contactEquations[i]; if (c.bodyA === someone.data || c.bodyB === someone.data) { var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis if (c.bodyA === someone.data) d *= -1; if (d > 0.5) result = true; } } return result;}this function works perfect but i always use players with circle shapes.. i saw that you didn't change the shape of your player so its most probably a rectangle..try player.body.setCircle(30); if this function still always returns FALSE try to play with this line:if (d > 0.5) result = true; try: if (d > 0.6) - in my game it works most reliable with d > 0.4 remove the restitution!! your player always bounces of the ground and therefore never realy touches it (at least not long enough)also your gravity is really low.. i work with gravity.y = 1400; good luck! Link to comment Share on other sites More sharing options...
Rotieh Posted January 16, 2015 Author Share Posted January 16, 2015 soooo.. if you are able to jump without the additional checks then which check is responsible for it ?? why so quiet about your findings? is it "game.time.now > jumpTimer" ?? does it work without it oris it "checkIfCanJump()" ??? if it's the checkIfCanJump() you need to debug this function further.. Yes, it works with "game.time.now > jumpTimer", allowing multiple jumps. You taught me that this part is not a problem, thank you. =) However, I wasn't able to make your function work =( if this function still always returns FALSE try to play with this line:if (d > 0.5) result = true; try: if (d > 0.6) - in my game it works most reliable with d > 0.4 I tried all values between 0 and 1 in your function and in the example function. I'm afraid I have not the amazing line of reasoning of you guys =( ARCADE physics was sooo easy =P Now I'm stucked in the first problem in P2JS try player.body.setCircle(30); remove the restitution!! your player always bounces of the ground and therefore never realy touches it (at least not long enough)also your gravity is really low.. i work with gravity.y = 1400; good luck! Thank you. I've changed the gravity, removed the restituition and I'm using player.body.setCircle(24). Thank you so much for the attention.I have not solved my problem yet, but I owe you a lot! PS.: I'm not familiar with English, and I'm trying to be very kind and courteous... So if something looks strange, forgive me... Thanks again! Link to comment Share on other sites More sharing options...
valueerror Posted January 17, 2015 Share Posted January 17, 2015 well.. you could just upload a zip file (attach it to your post) with your whole project.. i'll look into it in the evening, (try to) fix it and reupload it.. p2 is so much more powerful than arcade so if you get over the first blocking things it is definitely worth it Link to comment Share on other sites More sharing options...
Rotieh Posted January 17, 2015 Author Share Posted January 17, 2015 Thank you a lot, ValueError... There are comments in my mother tongue (is that the term?), just ignore... =P they are reminders to study latter. And I changed the sprites, I wanna do some kind of surprie... I don't know how to say it in English Thank you again! Link: http://www.mediafire.com/download/klfblqd2t8gcq4r/jogo4.zip Link to comment Share on other sites More sharing options...
valueerror Posted January 17, 2015 Share Posted January 17, 2015 here you are! the main problem was your phaser version.. i updated it and tuned your code a little bit here and there.. (look out for the comments) good luck have fun fixedgame.zip Link to comment Share on other sites More sharing options...
Rotieh Posted January 17, 2015 Author Share Posted January 17, 2015 Thank you so much! And Thanks to Sold Out Activist from the chat! Owe you a big favor! Thanks a lot! Link to comment Share on other sites More sharing options...
Recommended Posts