KevinnFtw

Members
  • Content Count

    52
  • Joined

  • Last visited

Everything posted by KevinnFtw

  1. KevinnFtw

    Fire bullet to predefined angle

    Hi Guys, So I'm flying a space ship using the accelerometer and gyro.js, with gyro.js I calculate the angle the ship is flying in. Now I want to fire a laser when I touch my mobile device's screen. The laser should fire in the direction the ship is flying in, no matter where the user touches the screen. I've searched for a couple of hours, but still didn't find a way to make this work. Any ideas? function fire(angle) { if(game.time.now > bulletTime) { bullet = bullets.getFirstExists(false); if(bullet) { bullet.reset(player.x, player.y + 8); bullet.rotation = this.game.physics.arcade.moveToObject(bullet, game.input.activePointer, 500); bulletTime = game.time.now + 200; } }}This is what I have got so far, and this works. But it doesn't fire in the direction the ship is flying in. ps. I've calculated the angle with the following code: var anglePlayer = Math.atan2(o.y * 20, o.x * 20);anglePlayer *= 180/Math.PI;anglePlayer = 180 - anglePlayer;player.angle = anglePlayer;
  2. Hi guys, So I'm trying to expand the game I'm making to be a multiplayer game using Node.js and Socket.io. The connection between server-side and client-side is working, however I don't know how to update the positions of the players. This is how it goes, whenever a player moves the socket.sessionid and coordinates are send to the server. The server then broadcasts this data to all the other clients except the client which sent the data. In the client-side that information gets decoded and stored in 2 variables (at this moment), a player variable (with the id), and a X variable (with the x value, duh). At this moment I really don't know how to update the positions of a certain sprite in the game. Are there any examples on how to create a basic multiplayer Phaser game using websockets? I'm kinda stuck. The code I'm using on the client side is as follows, it is very dirty atm since I'm experimenting.. The 'xChanged' function sends the new position to the server, and 'posChanged' reads the data that gets sent from the server. var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update: update});function preload() { game.load.spritesheet('player', 'img/ally_sprite.png', 64, 64); game.load.image('boss', 'img/boss.png'); game.load.image('bullet', 'img/bullet.png');}var player, boss, cursors, x, y, z, xTouch, yTouch, fireButton, bullets, bulletTime = 50, text, bulletsCount = 30;var players = {};var io = io.connect('http://192.168.0.13:3000');// server ipvar xPos, yPos, label, sock;function create() { game.renderer.clearBeforeRender = false; game.renderer.roundPixels = true; game.physics.startSystem(Phaser.Physics.ARCADE); player = game.add.sprite(game.world.centerX, game.world.centerY, 'player'); player.anchor.setTo(.5,.5); player.animations.add('fly'); player.animations.play('fly', 10, true); game.physics.enable(player, Phaser.Physics.ARCADE); player.enableBody = true; player.body.collideWorldBounds = true; // new player var pos = JSON.stringify({ player: io.socket.sessionid, x: game.world.centerX, y: game.world.centerY, angle: 0 }); socket.emit('newPos', pos); boss = game.add.sprite(game.centerX, game.centerY, 'boss'); boss.enableBody = true; boss.physicsBodyType = Phaser.Physics.ARCADE; bullets = game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(30, 'bullet'); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 1); bullets.setAll('outOfBoundsKill', true); game.stage.backgroundColor = '#ccc'; game.input.addPointer(); fireButton = game.input.pointer1; if(isMobile.any) { if(gyro.hasFeature('devicemotion')) { console.log('gyrojs loaded'); if(gyro.getFeatures().length > 0) { gyro.frequency = 10; gyro.startTracking(function(o) { var anglePlayer = Math.atan2(o.y, o.x); angleRadians = anglePlayer * Math.PI/180; anglePlayer *= 180/Math.PI; anglePlayer = 180 - anglePlayer; player.angle = game.math.wrapAngle(anglePlayer, false); if(fireButton.isDown) { fire(); } if(o.z < 9.5 || o.z > 10) { player.body.velocity.x -= o.x * 20; player.body.velocity.y += o.y * 20; } else { player.angle = 0; } console.log(player.body.velocity.x); // Send new position to server var newPos = JSON.stringify({ player: io.socket.sessionid, x: player.body.velocity.x, y: player.body.velocity.y, angle: player.angle }); socket.emit('newPos', newPos); }); } } else { // fallback als gyro.js niet werkt.. console.log('gyrojs not loaded'); window.addEventListener('devicemotion', function(event) { x = event.accelerationIncludingGravity.x; y = event.accelerationIncludingGravity.y; z = event.accelerationIncludingGravity.z; var anglePlayer = Math.atan2(y, x); anglePlayer *= 180/Math.PI; anglePlayer = 180 - anglePlayer; player.angle = game.math.wrapAngle(anglePlayer, false); if(fireButton.isDown) { fire(); } if(z < 9.5 || z > 10) { player.body.velocity.x -= x * 40; player.body.velocity.y += y * 40; } else { player.angle = 0; } // Send new position to server var newPos = JSON.stringify({ player: io.socket.sessionid, x: player.body.velocity.x, y: player.body.velocity.y, angle: player.angle }); socket.emit('newPos', newPos); var interval = 10; }); } } else { // Niet mobiel, bewegingen omzetten in keys console.log('Niet Mobiel'); cursors = game.input.keyboard.createCursorKeys(); } text = game.add.text(game.world.centerX, 50, "Bullets: 30", { font: "65px Arial", fill: "#000000", align: "center" }); text.anchor.setTo(0.5, 0.5); // Add new players to the screen socket.on('newPlayerwithPos', function(data) { var obj = JSON.parse(data); var xNew = obj.x; var yNew = obj.y; console.log(xNew, yNew); var p = game.add.sprite(xNew, yNew, 'player'); p.anchor.setTo(.5,.5); p.animations.add('fly'); p.animations.play('fly', 10, true); game.physics.enable(p, Phaser.Physics.ARCADE); p.enableBody = true; p.body.collideWorldBounds = true; });}function update() { player.body.velocity.setTo(0,0); if(!isMobile.any) { if(cursors.left.isDown) { player.body.velocity.x -= 40; } else if(cursors.right.isDown) { player.body.velocity.x += 40; var newPos = JSON.stringify({ x: player.x, player: io.socket.sessionid }); } if(game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).isDown) { fire(); } } // Get new positions from all other players socket.on('updatePos', function(data) { for(var playerData in data) { // update array of players var player = {}; player.name = data[playerData].sessionID; player.x = data[playerData].x; player.y = data[playerData].y; player.angle = data[playerData].angle; players[player.name] = player; } }); // loop through all players and draw sprites for(var playerData in players) { var p = ''; var playerX = players[playerData].x; var playerY = players[playerData].y; var playerAngle = players[playerData].angle; var p = game.add.sprite(playerX, playerY, 'player'); game.physics.enable(p, Phaser.Physics.ARCADE); p.enableBody = true; p.body.velocity.x -= playerX; p.body.velocity..y += playerY; p.angle = playerAngle; }}function fire() { if(game.time.now > bulletTime) { bullet = bullets.getFirstExists(false); if(bullet) { bullet.reset(player.body.x + 32, player.body.y + 32); bullet.rotation = player.rotation; game.physics.arcade.velocityFromRotation(player.rotation, 400, bullet.body.velocity); bulletTime = game.time.now + 50; // Update bullet counter bulletsCount --; text.setText("Bullets: " + bulletsCount); } }}
  3. KevinnFtw

    Sprite.tint semi transparent color?

    Edit: Nevermind, was being stupid... because it already multiplies the color I should just use more transparent color codes.
  4. Hi all, So I'm trying to add background music to my game, and I've provided phaser with a .ogg and a .mp3 file so it has a fallback. Problem is, the audio doesn't play/load at all in Google Chrome (latest build). In Firefox the audio is working fine for me. I've looked at some examples on examples.phaser.io and added the audio the same way they did there, like this: game.load.audio('backgroundMusic', ['assets/audio/TakingFlight.mp3', 'assets/audio/TakingFlight.ogg']);Later in the javascript I play the music using this code: backgroundMusic = game.add.audio('backgroundMusic');backgroundMusic.play('', 0, 1, true);Any suggestions / ideas? I've added the mp3 file of the background music in this post, wasn't allowed to upload the ogg.. TakingFlight.mp3
  5. Hi guys, Today I'm trying to get my game running in CocoonJS, the game does work perfectly in a normal desktop and mobile browsers, but I'm getting some errors in CocoonJS. The error says: Javascript Exception (Tag: 'load'): Error: Phaser.Loader. Invalid XML given at e.Loader.xmlLoadComplete(js/min/phaser.min.js:12:9823) at XMLHttpRequest.i.xmlURL._xhr.onload (js/min/phaser.min.js:12:7414) It's my first time using CocoonJS so I have no idea where the error is coming from, but it is coming from Phaser.js itself. Any ideas?
  6. KevinnFtw

    CocoonJS Javascript Exception (Tag: 'load')

    As far as I know my game itself is not using any xml, but I will look into it. Thanks!
  7. KevinnFtw

    Short way to removeAll() from state before switch?

    So you do like: sprite.destroy(); player.destroy(); enemy.destroy(); Do you destroy every element seperately?
  8. Hey guys, I'm having some issues switching between states. In the first run it all works fine, but then when I come back to a state I've already been at, like MainMenu, the design is all messed up. I think it is because not all sprites and stuff are removed when I switch states, that's why I added a this.world.removeAll() in my shutdown function. However that doesn't seem to do anything. Is there another short way to delete everything (except cache) from a state before switching to another state, or does it have to be done manually? I've attached 2 images, one of my MainMenu first time, and one of the MainMenu when I switch back to it (after the Game). Excuse me if it is a silly question, I've just recently started using states.. Thanks, Kevin
  9. KevinnFtw

    Short way to removeAll() from state before switch?

    Wait, isn't that what this.world.removeAll() is supposed to do? Doesn't that function destroy every element of the stage world?
  10. KevinnFtw

    Short way to removeAll() from state before switch?

    Thanks for your reply Loopeex :-) I've tried: shutdown: function() { this.world.removeAll(true);}But that doesn't make any difference unfortunately
  11. Hi guys, Maybe a silly question, but I recently started using States in my game and now I am converting my functions to work with states. The functions without parameters work fine, but the ones with parameters don't. For example, when I call: this.createBullet(x, y); I get the error: Uncaught typeError: undefined is not a function. The function createBullet(x, y) does exist, it is created like this: createBullet: function(x, y) { // blablabla content of function } p.s. these functions are in my Game.js state Can anyone tell me what I'm doing wrong? It has to be a silly mistake but I don't know how to fix it Thanks, Kevin
  12. KevinnFtw

    Phaser Functions with parameters in state issue

    Ahh, i was thinking about the 'this' reference, but didn't know what to replace it with. Edit: Awesome, it is working now! Thanks alot Rich and Lewster32 :-)
  13. KevinnFtw

    Phaser Functions with parameters in state issue

    I've uploaded the Game.js here: http://jsbin.com/senarewe/1 The problem is occuring at the createBullet() and createPlayer() functions.
  14. KevinnFtw

    Phaser Functions with parameters in state issue

    I get that, but some of my functions need to have a couple of variables to work. Normally, when I want to execute a certain function with parameters I would just call: function(a); I know that at states you have to use this.function if the function is inside of the game. So then I would call this.function(a) However when I do that I get the error Uncaught typeError: undefined is not a function The function(a) does exist. Edit:It might be that the context I'm using the function in is wrong, and not the function itself. I'm using it within a socket.on like: socket.on('newBullet', function(data) { this.function(data);});
  15. KevinnFtw

    Phaser Functions with parameters in state issue

    Yeah I know that, but most of my functions need one or more parameters. How should I handle them?
  16. KevinnFtw

    Phaser one-sided overlap detection?

    Hi guys, At the moment I'm having some issues detecting which sprite is overlapping another sprite. The overlap function works fine, but I would like to know which of the 2 sprites goes over the other sprite. I only want to execute a certain function if sprite A walks over sprite B, and not if sprite B walks over sprite A. Is there any way I could check this inside of the overlap function? Thanks, Kevin
  17. KevinnFtw

    Phaser one-sided overlap detection?

    That's a great solution as only one of them is moving at a time. Thanks!
  18. KevinnFtw

    Bringing text to front

    Hey Rich, I'm using Phaser 2.0.5, but there's no such option to bring text to the front/top. I also couldn't find it in the latest docs. Do I have to put all the text elements in a group and bring that to top or is there any other way? Thanks.
  19. KevinnFtw

    Invisible sprites ignore collision

    That is amazing, thanks alot Rich!
  20. KevinnFtw

    Invisible sprites ignore collision

    Hey guys, In my game I hide some sprites by setting their visible setting to false. Problem is, my collision detection still detects if these invisible sprites are getting hit. I would like to know how I can make the collision detection ignore them. Recently I've seen a option on these forums which was something like sprite.collision = false. I think it was an option to ignore the collision of a certain sprite. Too bad I can't find that post or option anymore, so that's why I'm making a new post about it. Hope anyone can help me, otherwise I'll have to write my own detection for just visible sprites but enabling some kind of option would be much easier. Thanks, Kevin
  21. KevinnFtw

    Invisible sprites ignore collision

    They are using ARCADE.
  22. KevinnFtw

    Invisible sprites ignore collision

    So I'll just need to do sprite.body = null? If I want to re-enable the body of the sprite in a later stage where the sprite is visible again, how should I do that?
  23. KevinnFtw

    Invisible sprites ignore collision

    Will try that, thanks
  24. KevinnFtw

    Group CreateMultiple infinite?

    Very nice example! That did the trick for me, thanks alot :-)
  25. KevinnFtw

    Group CreateMultiple infinite?

    Hi, I'm creating a shooter game, and I've followed the 'Space Invaders' example. Everything is working but one thing, I would like to have infinite ammo, so CreateMultiple has to be infinite. It requires a value, so I don't know how to make it actually infinite. Basically what I want to do is create a couple bullets and recycle them over and over again. Any suggestions on how I could do so you never run out of bullets? Thanks in advance, Kevin