Jonathan Silverblood Posted April 3, 2015 Share Posted April 3, 2015 Hi, I've been working a bit on getting some speech bubbles over my player and while the positioning is fairly straightforward I have run into alot of issues that makes me feel I have no clue what the heck I'm doing, so I thougth I'd ask some questions regarding update functions here. 1) When I create my player as "game.add.sprite(0, 0, null);" and add a postUpdate function to it, it completely stops moving. Why is this? 2) When I have have a speech bubble defined as "game.add.sprite(0, 0, null);" and add a postUpdate function to it, things work just fine. What is the difference here? 3) Inside a postUpdate on my speech bubble I do some math to calculate proper positions of child elements. I have no issues with getting the position of the child elements right, but since the player position is a float rather than an integer my tilemaps for the sides gets distorted. I thought I'd fix this by compensating the speech bubbles coordinates by taking the fraction form the parent (the player) and removing it form the speech bubbles position. For some reason this des not seem to apply properly and my speechbubble stutters back and forth. In what order do the preUpdate, update, postUpdate functions happen?// Create a new player.game.players[player_id] = game.engine.add.sprite(start_x, start_y, null);// Enable physics emulation for the playergame.physics.arcade.enable(game.players[player_id]);// Add a thought bubble..game.players[player_id].thought_bubble = game.engine.add.sprite(0, 0, null);game.players[player_id].addChild(game.players[player_id].thought_bubble);// Keep the text and orientation proper on every frame.game.players[player_id].thought_bubble.postUpdate = function(){ var offset_x = (this.world.x - Math.round(this.world.x)); var offset_y = (this.world.y - Math.round(this.world.y)); this.y = 0 - offset_y - Math.round(this.bg.height / 2) - 50; this.x = 0 - offset_x;} Link to comment Share on other sites More sharing options...
Jonathan Silverblood Posted April 3, 2015 Author Share Posted April 3, 2015 Answering my own post here. Looks like I made an assumption that was invalid in that I thought the postUpdate was an empty function that was there for my convenience. Turns out it has a content, so going to fiddle around with it some more. Link to comment Share on other sites More sharing options...
Berzee Posted April 3, 2015 Share Posted April 3, 2015 I think the stutter happens because this.world.x is changing every frame (it gets updated somewhere down the line based on the changes made to this.x). I believe the stuttering should stop if you calculate the offset using the parent position instead:var offset_x = (this.parent.world.x - Math.round(this.parent.world.x));var offset_y = (this.parent.world.y - Math.round(this.parent.world.y));this.y = 0 - offset_y - Math.round(this.bg.height / 2) - 50;this.x = 0 - offset_x;(If you didn't want to tamper with the postUpdate you could always do the same code in the game state update() loop too, probably)----Edit: From talking with you in the chatroom it sounds like my suggestions above don't help for tileSprites. But I will leave my failed attempt here for posterity nonetheless. =P Hopefully someone has a better answer. Link to comment Share on other sites More sharing options...
Jonathan Silverblood Posted April 3, 2015 Author Share Posted April 3, 2015 I tried following your advice and changed the postUpdate function to realign(), then called it at the end of the update loop. Also returned back to calculating it based on the parent coords, but neither helped. In case anyone feel like having a deeper look you can go to http://monsterbitar.se/ and thecode is in source/game.js Link to comment Share on other sites More sharing options...
Recommended Posts