Lutcikaur Posted September 25, 2014 Share Posted September 25, 2014 So I have my game going, and I've avoided groups for the most part because in the past I've had issues making group members interact nicely. Now I have a z-ordering problem and using the information from these three threads I've tried to add all of my loot drops into a group created at the start before the player, So when an item drop is spawned it cannot be drawn ontop of the player. The problem with this is that The objects dont behave how they did before. I have an enemy that on death, it spawns a loot drop with x,y = to the enemies x,y, so normally it spawns right where he died. When i do that same then and then call the following function, the items drop in random locations, and I can only find them by traveling to the top left of the map where they eventually hit bounds because they seem to move with my player.this.game.dropGroup.add(this.entity);I have them collide with world bounds, so when i hit the top left of the map they stay at the top left of my camera, and stay there regardless of where i move.I was actually going to attempt to add all the enemies, player, bullets ,backgroupds into seperate groups too for perfect z-ordering, but I cant even get this to work. Link to comment Share on other sites More sharing options...
lewster32 Posted September 25, 2014 Share Posted September 25, 2014 It's difficult to visualise what's going on here - any chance you could upload a demo? I think maybe you're still struggling with the way scene graphs work, and possibly there are objects being made children of other objects when they perhaps shouldn't be. Remember that the x, y, scale and rotation properties (and a few others) are always relative to the object's parent in a scene graph. An object at 10, 10 will be 10 pixels to the right and below the screen, but if that object is added to another object or group which is at 10, 10 itself, the first object will suddenly move to 20, 20, as it's now at 10+10, 10+10. If the parent is itself added to another object at 10, 10, the first object will move again to 30, 30, as it's now 10+10+10, 10+10+10. As you go deeper and deeper, each object will be positioned in the world by adding its position to all of the positions of its ancestors until the renderer reaches the root - usually game.world. Link to comment Share on other sites More sharing options...
Lutcikaur Posted September 25, 2014 Author Share Posted September 25, 2014 I don't think that I can upload this nicely, making a jsfiddle of it would be difficult, and trying to reproduce it by making a new game+area+group it works nicely. In my game.js i have this.dropGroup = this.game.add.group(this,null,'drops');//....this.enemies.push(new Main.Enemy_RedKnight(i,this,x,y));//....In enemy.js i have Main.Enemy_RedKnight = function (name, game, x, y){this.game = game;this.spriteName = 'redKnight';this.bulletSprite = 'bullet';Main.Entity.call(this, name, game, x, y, this.spriteName);this.entity.body.immovable=true;this.attack = new Main.Attack_RedKnight(this.game,this,this.bulletSprite, this.game.addEnemyBullets(this.bulletSprite));};//...Main.Enemy_RedKnight.prototype.death = function(gotHit, theBullet) {this.game.loot.push(new Main.Drop_HealthPack('healthPack from ' + this.entity.name,this.game,this.entity.body.x,this.entity.body.y, this));this.alive = false;this.entity.kill();}; [edit::: I cant the the above chunk of text into the code block above. odd] entity.jsMain.Entity = function (name, game, x, y, spriteName){ this.game = game; this.spawnX = x; this.spawnY = y;this.entity = this.game.add.sprite(x,y,spriteName);this.game.physics.enable(this.entity, Phaser.Physics.ARCADE);this.hpBarBack = this.game.add.sprite(-22,-this.entity.body.halfHeight-13,'entityHpBarBack');this.hpBar = this.game.add.sprite(-21,-this.entity.body.halfHeight-12,'entityHpBar');this.hpBar.baseWidth = this.hpBar.width;this.hpBar.cropEnabled=true;this.entity.addChild(this.hpBarBack);this.entity.addChild(this.hpBar);this.entity.origin = this;this.entity.animations.add('down',[0,1,2], 10,true);this.entity.animations.add('right',[6,7,8], 10,true);this.entity.animations.add('left',[9,10,11], 10,true);this.entity.animations.add('up',[3,4,5], 10,true);this.entity.anchor.setTo(0.5, 0.5);this.entity.body.collideWorldBounds=true;this.entity.body.immovable=true;};drop_healthpack.js Main.Drop_HealthPack = function (name, game, x, y, origin){this.game = game;this.itemName = 'healthPack';Main.Drop.call(this, name, game, x, y, this.itemName, origin);this.autoGrab = true;};drop.js Main.Drop = function (name, game, x, y, spriteName, origin){ this.game = game;this.alive = true;this.origin = origin;this.entity = this.game.add.sprite(0,0,spriteName);this.entity.origin = this;this.entity.name = name.toString();this.game.physics.enable(this.entity, Phaser.Physics.ARCADE);this.entity.body.collideWorldBounds=true;this.entity.body.immovable=true;this.autoGrab = null;// This is the problem section. I want to add this line and have everything work as normal.this.game.dropGroup.add(this.entity);};I think those are the relevant functions of every related file, I have no idea if you can help with just this information, but I appreciate the attempt immensely. edit; also with this, having >>>> this.entity = this.game.add.sprite(0,0,spriteName); <<<< in the drop.js forces the healthpacks to drop at the very top left of my screen. The healthpacks are anchored at 0,0 so they are sitting with their top left corner at the top left corner of my screen. It's definitely something to do with the parent of the dropGroup, I just don't know what. Link to comment Share on other sites More sharing options...
lewster32 Posted September 25, 2014 Share Posted September 25, 2014 One thing I can see straight away is the parameters for game.add.group are wrong:this.dropGroup = this.game.add.group(this,null,'drops');Should be:this.dropGroup = this.game.add.group(this.game.world,'drops');Though given the name of groups isn't really used, just this would do:this.dropGroup = this.game.add.group(); Link to comment Share on other sites More sharing options...
Lutcikaur Posted September 25, 2014 Author Share Posted September 25, 2014 Alright. going to crawl under my hole now. . . . Sigh.. One of the things I had tried was this.dropGroup = this.game.add.group(this.game.world,'drops');and even in trying it again, It dosn't work. This however does:this.dropGroup = this.game.add.group();Man... I spent most of yesterday trying to get this group thing to work. All i had to do was create the group without any parameters. Thankyou. I guess 'difficult' problems have simple solutions. Link to comment Share on other sites More sharing options...
lewster32 Posted September 25, 2014 Share Posted September 25, 2014 Hehe don't worry about it my friend, happens to us all - and it's what makes programming fun! Link to comment Share on other sites More sharing options...
Recommended Posts