Search the Community
Showing results for tags 'for loop'.
-
Hi, I am new to coding and phaser 3. Here is what I am trying to do- I am have a world map that is draggable, and I want to add markers on the map of the cities I want to add. The cities are all stored in an array, myCities2. I have a plan to add a lot of cities and adding them through a function or loop will make it a lot easier than applying each city individually. Each city will link to a "demo scene" where the city name, city image and other relevant information will be passed and the city can be explored. I have build a lot of this in another file but each city and click event hast to be copy pasted and so on creating hundreds and hundres of lines of code and repetition, I am sure there is a better way and I know coding has a solution, as my coding friends tell me. Efficient code is my goal here. I have tried forEach(), and for loops but can't get them to add the images to the container with the map - as the map is dragged the cities need to be dragged along with it... Any suggestions? Thanks, Your help is greatly appreciated. myCities.js
-
var game = new Phaser.Game(1024,768,Phaser.AUTO, 'space game', {preload: preload, create: create, update: update}); var ships; var ship; function preload(){ game.load.image('spaceship', 'assets/spaceship_sprite.png'); game.load.image('menu', 'assets/menu.png'); } function create(){ ship = game.add.sprite(500,680, 'spaceship'); game.physics.arcade.enable(ship); ship.scale.setTo(0.5,0.5); //ship2 = myship(game, 'spaceship', 600, 200); ships = game.add.group(); for (var i = 0; i < 0; i++){ //var sx = 100; ships.create(600, 200, 'spaceship'); //myship(game, 'spaceship', 500+sx, 200); //sx += 100; //game.add.sprite(500, 200, 'spaceship'); } cursors = game.input.keyboard.createCursorKeys(); pauseKey = game.input.keyboard.addKey(Phaser.Keyboard.ONE); pauseKey.onDown.add(addMenu, this); game.input.keyboard.removeKeyCapture(Phaser.Keyboard.ONE); } function update(){ if(cursors.left.isDown){ ship.body.x += -7; } if(cursors.right.isDown){ ship.body.x += 7; } } function addMenu(){ game.add.sprite(100,100,'menu'); } 1. I want to add a few sprites of a ship into the 'ships' group. But for loop doesn't work. I'm folowing this example: https://phaser.io/examples/v2/groups/add-a-sprite-to-group#gv even If I try to add object from external script. I've tried to create a few ships of 'myship' without the 'for loop' and it works fine. 2. If I uncomment this line: //ship2 = myship(game, 'spaceship', 600, 200), my 'ship' doesn't move (key input doesn't work). What am I doing wrong? Sorry for my poor english.
- 1 reply
-
- phaser
- sprite group
-
(and 1 more)
Tagged with:
-
I need some help to optimize algorithm. I have a matrix of sprites and I am highlighting a part of it. It can be rectangle or square it doesn't matter. I need to hide all sprites of this matrix that do not belong to the given area. I have this function which is highly non-optimized i.e 4 nested "for" cycles: hideMatrixArea: function(slotTwoDimArray, row, col, numRows, numCols) { var i = 0, j = 0, k = 0, t = 0, matrixWidth = slotTwoDimArray.length, matrixHeigth = slotTwoDimArray[0].length; for (i = 0; i < matrixWidth; i += 1, k += 1) { for (j = 0; j < matrixHeigth; j += 1, t += 1) { for (k = row; k < row + numRows; k += 1) { for (t = col; t < col + numCols; t += 1) { if (!(k === i && t === j)) { console.log("hello"); this.hideSlot(slotTwoDimArray, i, j); } } } } } return slotTwoDimArray; }, It prints "hello" 15600 times which is a lot. How can I hide slots that are not in the space (row, col) -> (row + numRows, col + numCols) i.e the inverse without using nested "for" loops? Any ideas?
-
I'm trying to make a silly RPG in phaser.js, right now I am working on the battle system and the battle menus. However there is a problem with the item inventory. I have made so that the texts for the names of the items that are in my inventory appear when I open the inventory and disappear when I close it or after I have used an item (and then I delete that item using splice since all the items are in a list). However, when I use an Item, the text for the name of that item does not disappear in the inventory when I open it again (but I cannot use the item at least). And also, the text for the name of the last item in the list stays on the sceern even when I haven't open the inventory. First i thought it was because the for-loop I use to create all the "name texts" for all items in the list was in the create function, but I tested puting it in the update function but it didn't change anything. I just want a simple item inventory that you find in almost every RPG. Can someone please help me with this? Some variabels in the code are on swedish. Here's a translation of some of those words to make it easier for you: Spel = Game Pil = Arrow / Cursor Pilar = Arrows / Cursors Here is the code (I couldn't attach the file to this message for some reason) : <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>My Brain</title> <script type="text/javascript" src="js/phaser.js"></script> <style type ="text/css"> body { margin: 0; } </style> </head> <body onload="start()"> <script type="text/javascript"> var spel = new Phaser.Game(1000, 700, Phaser.AUTO, 'phaser'); var modes = {}; function start() { spel.state.add("Game Over", modes.gameover); spel.state.add('battle', modes.battle); spel.state.start('battle') } //---------------------------------- Battle mode ------------------------------------------ modes.battle = { preload: function() { spel.load.image('battleground', 'Sprites/background.png'); spel.load.image('triangle', 'Sprites/EvilTriangle2.png'); var pil; Turn = 1; chooseCommand = true; chooseItem = false; chooseMagic = false; playerlife = 1000; enemylife = 300; attackNow = false; improvizeNow = false; magicNow = false; itemNow = false; USEspell = false; USEheal = false; spellingdecider = ""; waittime = 1 spellTime = 0 itemvalue = 0 }, create: function () { //------ Sprites ------- spel.add.sprite(0, 0, 'battleground'); triangle = spel.add.sprite(360, 200, 'triangle'); //------ Menyn ------ menyATTACK = spel.add.text(225, 540, 'Attack', { fontSize: '35px', fill: '#ffffff' }); menyITEMS = spel.add.text(655, 540, 'Items', { fontSize: '35px', fill: '#ffffff' }); menyMAGIC = spel.add.text(230, 620, 'Magic', { fontSize: '35px', fill: '#ffffff' }); menyIMPROVIZE = spel.add.text(575, 620, 'Improvize!', { fontSize: '35px', fill: '#ffffff' }); //--------- Magic meny ----------- HealMAGIC = spel.add.text(255, 540, 'Heal', { fontSize: '35px', fill: '#ffffff' }); deCORNERizeMAGIC = spel.add.text(500, 540, 'deCORNERize', { fontSize: '35px', fill: '#ffffff' }); SpellMAGIC = spel.add.text(245, 620, 'Spell', { fontSize: '35px', fill: '#ffffff' }); BackMAGIC = spel.add.text(655, 620, 'Back', { fontSize: '35px', fill: '#ffffff' }); //------ Items Meny ------ itemList = []; itemList.push(new item("Chocolate bar", 600, 50)); itemList.push(new item("Chokolate bar", 600, 50)); itemList.push(new item("Delicious Yoghurt", 600, 50)); itemList.push(new item("Coffee", 600, 50)); itemList.push(new item("Pencil Sharperner", 600, 50)); itemList.push(new item("Scary mask", 600, 50)); itemList.push(new item("Old reciept", 600, 50)); itemList.push(new item("Game Guy color", 600, 50)); for (var i = 0; i < itemList.length; i++) { itemList.y += i*50; if (i > 4) { itemList.x += 200; itemList.y = 50*(i-4); } } //----- skapar en text grupp till alla items så att jag kan deleta all text sen på ett enkelt sätt ------ textgroup = spel.add.group(); //------------------------------------------------------------------------------------------------------- for (var i = 0; i < itemList.length; i++) { ItemTEXT = spel.add.text(itemList.x, itemList.y, itemList.name, { fontSize: '20px', fill: '#ffffff' }); textgroup.add(ItemTEXT); } ItemListBack = spel.add.text(800, 300, "Back", { fontSize: '20px', fill: '#ffffff' }); //---- Pilar ---- pil = ""; pil = spel.add.text(350, 540, '<', { fontSize: '35px', fill: '#ffffff' }); Itempil = ""; Itempil = spel.add.text(580, 50, '>', { fontSize: '20px', fill: '#ffffff' }); //------------------ Fiendens Lifebar ------------------ enemylifeTEXT = ""; enemylifeTEXT = spel.add.text(20, 10, 'Enemy HP: 300', { fontSize: '25px', fill: '#ffffff' }); //------ Din Lifebar ------ playerlifeTEXT = ""; playerlifeTEXT = spel.add.text(770, 480, 'You: 1000/1000', { fontSize: '25px', fill: '#ffffff' }); //----- Knappar som man använder ------ pilar = spel.input.keyboard.createCursorKeys(); accept = spel.input.keyboard.addKey(Phaser.Keyboard.Z); }, update: function () { //------------- Gör så att alla item får sitt rätta namn -------------------- if (chooseItem == false) { for (var i = 0; i < itemList.length; i++) { textgroup.getAt(i).visible = false; } Itempil.visible = false; ItemListBack.visible = false; } // ----- Vad du kan göra på din tur ----- if (Turn == 1) { if (chooseCommand == true) { //Funktion åt pilen if (pilar.right.isDown && pil.x == 350) { pil.x = 765; } if (pilar.left.isDown && pil.x == 765) { pil.x = 350; } if (pilar.up.isDown && pil.y == 620) { pil.y = 540; } if (pilar.down.isDown && pil.y == 540) { pil.y = 620; } } // ----------------------------- Välj items i item meny ----------------------------------- if (chooseItem == true) { Itempil.visible = true; ItemListBack.visible = true; chooseCommand = false; for (var i = 0; i < itemList.length; i++) { textgroup.getAt(i).visible = true; } // Rör Itempilen upp och ner (med en egen sorts break) if (pilar.down.isDown) { Itempil.y += 50; pilar.down.isDown = false; } if (pilar.up.isDown) { Itempil.y -= 50; pilar.up.isDown = false; } // Om pilen går utanför toppen/botten går den tillbaka till botten/toppen if (Itempil.y < 50) { Itempil.y = 300; } if (Itempil.y > 300) { Itempil.y = 50; } // Rör Itempilen höger och vänster (med en egen sorts break) if (pilar.right.isDown) { Itempil.x += 200; pilar.right.isDown = false; } if (pilar.left.isDown) { Itempil.x -= 200; pilar.left.isDown = false; } // Om pilen går utanför någon av sidorna går den tillbaka till motsatta sida if (Itempil.x < 580) { Itempil.x = 780; } if (Itempil.x > 780) { Itempil.x = 580; } if (Itempil.y == 300) { Itempil.x = 780; } for (var i = 0; i < itemList.length; i++) { if (Itempil.x == itemList.x - 20 && Itempil.y == itemList.y && accept.isDown) { //----- HP/MP points increaser ----- if (itemList.name == "Chocolate bar") { itemuseTEXT = spel.add.text(350, 100, 'You ate the chocolate bar...', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 1; } if (itemList.name == "Chokolate bar") { itemuseTEXT = spel.add.text(100, 350, 'Wait, the word chocolate is mispelled on this chocolate bar!', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 2; } if (itemList.name == "Delicious Yoghurt") { itemuseTEXT = spel.add.text(100, 350, 'You took your spoon and swooped up some of the "Delicious ougurt".', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 3; } if (itemList.name == "Coffee") { itemuseTEXT = spel.add.text(100, 350, 'You did it4', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 4; } //------ Stats changer items ----- if (itemList.name == "Pencil Sharperner") { itemuseTEXT = spel.add.text(100, 350, 'You did it5', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 5; } if (itemList.name == "Scary mask") { itemuseTEXT = spel.add.text(200, 200, 'You did it6', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 6; } //----- Useless/funny items ----- if (itemList.name == "Old reciept") { itemuseTEXT = spel.add.text(200, 200, 'You did it7', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 7; } if (itemList.name == "Game Guy color") { itemuseTEXT = spel.add.text(200, 200, 'You did it8', { fontSize: '35px', fill: '#ffffff' }); itemvalue = 8; } itemList.splice(i, 1); textgroup.remove(i, true); itemNow = true; Turn = 2; attackwait(); } } if (Itempil.y == 300 && Itempil.x == 780 && accept.isDown) { chooseItem = false; chooseCommand = true; accept.isDown = false; } } //--------- Attack commando / Heal Magic ------------ if (pil.x == 350 && pil.y == 540 && accept.isDown) { if (chooseMagic == false) { attackNow = true; Turn = 2; attackwait(); MYattackTEXT = spel.add.text(400, 50, 'You charged foward...', { fontSize: '35px', fill: '#ffffff' }); } if (chooseMagic == true) { magicNow = true; USEheal = true; Turn = 2; attackwait(); healTEXT = spel.add.text(400, 50, 'You used healing...', { fontSize: '35px', fill: '#ffffff' }); } } // ------------ Item commando / deCORNERize Magic ----------- if (pil.x == 765 && pil.y == 540 && accept.isDown) { if (chooseMagic == false) { chooseCommand = false; chooseItem = true; accept.isDown = false } if (chooseMagic == true) { //magicNow = true; //USEdeCORNERize = true; //Turn = 2; //attackwait(); deCORNERizerTEXT = spel.add.text(300, 300, 'deCORNERize', { fontSize: '35px', fill: '#ffffff' }); } } // ---------- Improvize commando ----------- if (pil.x == 765 && pil.y == 620 && accept.isDown) { if (chooseMagic == false) { var ImprovOpptions = ["You did a litte bellydance...", "You made a inappropriate joke...", "You recited the entire alphabet backwards...", "You tried to moonwalk..."]; var Improvdecider = Math.round(Math.random() * 3) ImprovizeText = ImprovOpptions[Improvdecider]; improvizeNow = true; Turn = 2; attackwait(); TheImprovizeTEXT = spel.add.text(200, 50, ImprovizeText, { fontSize: '35px', fill: '#ffffff' }); } if (chooseMagic == true) { chooseMagic = false; pil.x = 350; pil.y = 620; accept.isDown = false; } } // ------------ Magic commando / Spell Magic ----------- if (pil.x == 350 && pil.y == 620 && accept.isDown) { if (chooseMagic == false) { chooseMagic = true; pil.x = 350; pil.y = 540; accept.isDown = false; } if (chooseMagic == true && accept.isDown) { if (spellTime == 0) { spellingTEXT = spel.add.text(100, 100, 'Okay. Dog... D, O, G.', { fontSize: '35px', fill: '#ffffff' }); } if (spellTime > 0) { var spelling = ["Apple... A, P, P, L, E.", "Brian... B, R, I, A, N.", "Toys... T, O, Y, S", "Pseudopseudohypoparathyroidism... Uuuh... P, S... Q?"]; spellingdecider = Math.round(Math.random() * 3) TheSpelling = spelling[spellingdecider]; spellingTEXT = spel.add.text(100, 100, TheSpelling, { fontSize: '35px', fill: '#ffffff' }); } magicNow = true; USEspell = true; Turn = 2; attackwait(); } } //--------- Gör så att alla magi attacker som man kan välja visas ----------- if (chooseMagic == true) { deCORNERizeMAGIC.visible = true; HealMAGIC.visible = true; SpellMAGIC.visible = true; BackMAGIC.visible = true; menyATTACK.visible = false; menyITEMS.visible = false; menyMAGIC.visible = false; menyIMPROVIZE.visible = false; } if (chooseMagic == false) { deCORNERizeMAGIC.visible = false; HealMAGIC.visible = false; SpellMAGIC.visible = false; BackMAGIC.visible = false; menyATTACK.visible = true; menyITEMS.visible = true; menyMAGIC.visible = true; menyIMPROVIZE.visible = true; } } //--------------------------------------- Vad som händer vid olika turer (förutom tur = 1) ----------------------------------- if (Turn == 3 && accept.isDown) { if (attackNow == true) { MYattackTEXT.kill(); ENEMYdamageTEXT.kill(); attackNow = false; } if (improvizeNow == true) { TheImprovizeTEXT.kill(); TheReactionTEXT.kill(); improvizeNow = false; } if (magicNow == true) { if (USEspell == true) { spellingTEXT.kill(); SpellingAFTERTEXT.kill(); USEspell == false; } if (USEheal == true) { healTEXT.kill(); healingTEXT.kill(); USEheal = false; } magicNow = false; } if (itemNow == true) { itemuseTEXT.kill(); itemeffetTEXT.kill(); itemvalue = 0 itemNow = false; } Turn = 4; ENEMYattackTEXT = spel.add.text(70, 50, 'The Triangle pointed out that your shoes were untied...', { fontSize: '35px', fill: '#ffffff' }); attackwait(); } if (Turn == 5 && accept.isDown) { if (chooseItem == true) { chooseItem = false; } Turn = 1; ENEMYattackTEXT.kill(); MYdamageTEXT.kill(); accept.isDown = false; chooseMagic = false; chooseCommand = true; pil.x = 350; pil.y = 540; } }, } //--------------------------- Game over mode ------------------------------------ modes.gameover = { preload: function () { spel.load.image("Game over screen", "Phaser/Sprites/ded.png") }, create: function () { }, update: function () { }, } //----------------------- Funktion som håller koll på namnet och placerningen till itemet --------------------------- function item(name, x, y) { this.name = name; this.x = x; this.y = y; return name; } //------------------------- Funktionen som ger skada till fienden ---------------------------- function attackCommand() { Mydamage = Math.round(Math.random() * 50) + 100; enemylife -= Mydamage; enemylifeTEXT.text = "Enemy HP: " + enemylife; ENEMYdamageTEXT = spel.add.text(350, 100, 'The Triangle Recived ' + Mydamage + ' hit points.', { fontSize: '35px', fill: '#ffffff' }); } //------------------------------- Items funktioner efter användning ------------------------- function itemCommand() { if (itemvalue == 1) { playerlife += 500; itemeffetTEXT = spel.add.text(350, 100, "It was delisous in a chocolate kid of way. You restored 500 HP.", { fontSize: '35px', fill: '#ffffff' }); if (playerlife > 1000) { playerlife = 1000 } playerlifeTEXT.text = "You: " + playerlife + "/1000"; } if (itemvalue == 2) { playerlife += 500; itemeffetTEXT = spel.add.text(350, 100, "It dosen't matter, is just as delicious. You restored 500 HP.", { fontSize: '35px', fill: '#ffffff' }); if (playerlife > 1000) { playerlife = 1000 } playerlifeTEXT.text = "You: " + playerlife + "/1000"; } if (itemvalue == 3) { playerlife += 250; itemeffetTEXT = spel.add.text(350, 100, "Ironacally it taste great, but not delicious. You restored 500 HP.", { fontSize: '35px', fill: '#ffffff' }); if (playerlife > 1000) { playerlife = 1000 } playerlifeTEXT.text = "You: " + playerlife + "/1000"; } if (itemvalue == 4) { playerlife += 500; itemeffetTEXT = spel.add.text(350, 100, "Ironacally it taste great, but not delicious. You restored 500 HP.", { fontSize: '35px', fill: '#ffffff' }); } } // ------------------------ Alla Magi attackers funktioner -------------------------- function magicCommand() { //if (USEdeCORNERize == true) { //} if (USEheal == true) { HealEffect = Math.round(Math.random() * 200) + 100; playerlife += HealEffect; if (playerlife > 1000) { playerlife = 1000 } playerlifeTEXT.text = "You: " + playerlife + "/1000"; healingTEXT = spel.add.text(400, 100, 'You restored ' + HealEffect + ' HP.', { fontSize: '35px', fill: '#ffffff' }); } else if (USEspell == true) { if (spellTime == 0) { SpellingAFTERTEXT = spel.add.text(400, 100, '...What? YOU told me to spell...', { fontSize: '35px', fill: '#ffffff' }); } if (spellTime > 0) { if (spellingdecider < 5) { SpellingAFTERTEXT = spel.add.text(400, 100, '...Did you really expect something else would happend?', { fontSize: '35px', fill: '#ffffff' }); } if (spellingdecider > 5) { SpellingAFTERTEXT = spel.add.text(400, 100, "You know what? Lets just fight this guy instead. ", { fontSize: '35px', fill: '#ffffff' }); } } spellTime += 1; } } // -------------------------- Improvize Funktionen ----------------------------- function Monsterreaction() { var reaction = ["The Triangle was mildly amused.", "The triangle was impressed... But not really...", "The triangle pretended to care."]; var reactiondecider = Math.round(Math.random() * 2) reactionText = reaction[reactiondecider]; TheReactionTEXT = spel.add.text(100, 100, reactionText, { fontSize: '35px', fill: '#ffffff' }); } //-------------------- Funktion för vad fienden gör på sin tur---------------------- function EnemyTurn() { Triangledamage = Math.round(Math.random() * 50) + 200; playerlife -= Triangledamage; if (playerlife < 0) { playerlife = 0 } playerlifeTEXT.text = "You: " + playerlife + "/1000"; MYdamageTEXT = spel.add.text(350, 100, 'You Recived ' + Triangledamage + ' hit points.', { fontSize: '35px', fill: '#ffffff' }); } //------------------- Intervallen som gör att det blir en paus mellan attackerna ------------------------- function attackwait() { time = setInterval( function () { waittime -= 1 if (waittime <= 0) { if (Turn == 2) { if (attackNow == true) { attackCommand(); } if (improvizeNow == true) { Monsterreaction(); } if (magicNow == true) { magicCommand(); } if (itemNow == true) { itemCommand(); } Turn = 3; } if (Turn == 4) { EnemyTurn(); Turn = 5; } clearInterval(time); waittime = 1; } }, 1000 ) } </script> </body> </html>