vitorregisr Posted July 17, 2018 Share Posted July 17, 2018 //the code that i tried to use was with a little error, as you can se on the first snippet code, i didnt change the group was colliding and not the specific body[x]. //the code below is working! =D the fireElementals.group to fireElementals.bodys[x] for(var x=0; x<= fireElementals.bodys.length -1; x++){ bulletsKnife.forEachAlive(function (bulletKnife) { game.physics.arcade.collide(bulletKnife, fireElementals.bodys[x], function () { bulletKnife.kill(); fireElementals.hited(1,fireElementals.bodys[x]); }, null, this); }); } Hello guys! I'm trying to automate and optimize my game code. There is a lot of monsters and enemies, that i'm manual adding and creating, and i am thinking in a code that automate it, i will show you, and after i will explain the problem that i'm having. //create the object that will receive the things var fireElementals = new Object(); //the presets that are called before the gen fireElementals.presets = function(){ this.group = game.add.group(); game.physics.arcade.enable(this.group); this.group.enableBody = true; this.bodys = new Array(); } //the possets that are called after the gen, it add call the animations to all group. fireElementals.possets = function(){ this.group.callAll('animations.add', 'animations', 'fly', [0 , 1, 2, 3, 4], 9, true); this.group.callAll('animations.add', 'animations', 'dead', [0, 1, 2, 3, 4], 10, true); this.group.callAll('animations.add', 'animations', 'attack', [20, 21, 23, 24,25], 9, true); this.group.callAll('animations.add', 'animations', 'hited', [26,27,29,29,30], 9, true); this.group.callAll('play', null, 'fly'); this.group.setAll('body.immovable', true); } //the code that gen fireElementals.gen = function(x,y,maxHp){ this.bodys.push(this.group.create(x,y, 'fireElemental')); this.bodys[this.bodys.length-1].body.setSize(70, 90, 50, 35); this.bodys[this.bodys.length -1].maxHp = maxHp; this.bodys[this.bodys.length -1].hp = maxHp; } //the code that are called when the group fireElementals.group collide with the bullet fireElementals.hited = function(demage,body){ body.hp -= demage; console.log(body.hp) if(body.hp < 0){ body.kill(); } } //this code block is on att function, and makes the colision between bullets and the group //HERE IS THE PROBLEM!!!! bulletsKnife.forEachAlive(function (bulletKnife) { game.physics.arcade.collide(bulletKnife, fireElementals.group, function () { bulletKnife.kill(); fireElementals.hited(1,/*i need to send the body that are hited, for example: fireElementals.bodys[0]*/); }, null, this); }); //i tried it, but dont work for(var x=0; x<= fireElementals.bodys.length -1; x++){ bulletsKnife.forEachAlive(function (bulletKnife) { game.physics.arcade.collide(bulletKnife, fireElementals.group, function () { bulletKnife.kill(); fireElementals.hited(1,fireElementals.bodys[x]); }, null, this); }); } //this is how i'm calling the functions to gen the elementals on the state fireElementals.presets(); fireElementals.gen(200,200,10); fireElementals.gen(700,200,10); fireElementals.possets(); So, this is what i'm having: The elementals are being generated right, its ok, but the problem is when i atack it, as you can see what I explained in the code, i need to remove only the health that who was been hited. what do you guys thing about my code? can i improve it? what can i do to fix it? how can i send the right parameter? thank you guys, and sorry for my really bad english, im brasilian. here you can see my game with the code totally manual and without objects and not automatezed. Its working, but make the game toiful to add new stages... https://vitorregisrr.github.io/adventuretimeHellRunning/ Link to comment Share on other sites More sharing options...
Recommended Posts