Search the Community
Showing results for tags 'variable'.
-
I am trying to create an adventure game. Very new to Phaser and Js. I've been through multiple tutorials and am now trying to make my way on my own. If you imagine a stage that can be returned to at any time, I need to know if the coins on that stage were collected before. I can write individual functions for each coin successfully: var State1 = { preload: function(){ game.load.image('coin','coin.png'); }, create: function(){ //note these vars are declared in Main state numCoins = 0; //the array that will tell me if the coin has already been collected. that check hasn't been written yet. coinsCollected = []; this.coinText =game.add.text(game.world.width-20, 30, numCoins, {font: "26px Amatic SC"}); //single coins to collect on the stage. trying 2 different ways to show, as button and as sprite this.coin1=game.add.button(600, 300, 'coin', this.getCoin1, this); var coin2 = game.add.sprite(350, 250, 'coin'); coin2.inputEnabled=true; coin2.events.onInputDown.add(this.getCoin, this); var coin3 = game.add.sprite(400, 100, 'coin'); coin3.inputEnabled=true; coin3.events.onInputDown.add(this.getCoin, this); }, getCoin1: function(coin){ numCoins ++; this.coinText.text=numCoins; //which coins have been collected coinsCollected.push('coin1'); console.log(coinsCollected); coin.kill(); }, getCoin2: function(coin){ numCoins ++; this.coinText.text=numCoins; coinsCollected.push('coin2'); console.log(coinsCollected); coin.kill(); }, So that works fine, but obviously I don't want to write out a hundred "getCoin" functions. How do I pass the name of the coin to a single getCoin function so I can store it in an array? so getCoin: function(coin){ numcoins ++; this.coinText.text=numCoins; coinsCollected.push(coinNAME); }
-
Hello, I have a form built using PHP, and need to pass the text input value to an external .js file. Here is the PHP code: <?php $brsh_size = 1; ?> <span> <div><span><center><input class = 'sp-flat' type="color" id="picker" value="#000" act="get-color" /></center></div> <center class='font-brush'>Brush Size:</a><input type="text" size="3" maxlength="3" id="br_size" act="get-brushsize" value="<?php echo $brsh_size;?>"></div></center> </span> I have a case is an external .js file where the case is called correctly: case 'get-brushsize': //var myPhpValue = $("#brsh_size").val(); //console.log(myPhpValue); //var brush_size_i = document.getElementById("br_size").value; //console.log("brush size is " + brush_size_i); break; I left some of my trial code for reference, but please ignore as I've gotten much farther with this - but run into issues. I've tried every method on stack overflow and other sites, and am unable to '_GET' the value of the PHP variable (not my first choice) but preferably the text input from the input 'id'. Any help is appreciated. Thanks, DB
-
Hey guys, I have a quick question about 'Groups' as I'm kinda new to this Phaser HTML5 game dev. So my plan is to create multiple groups and have control over each one of them. First thing that came into my mind is to create a 'for loop' and be done with! Problem is that I don't know how to assign different names for a variable (wave).Example: Instead of doing this: this.wave00 = game.add.group(); this.wave00.enableBody = true; this.wave00.physicsBodyType = Phaser.Physics.ARCADE; this.wave01 = game.add.group(); this.wave01.enableBody = true; this.wave01.physicsBodyType = Phaser.Physics.ARCADE; this.wave02 = game.add.group(); this.wave02.enableBody = true; this.wave02.physicsBodyType = Phaser.Physics.ARCADE; I am trying to do this: for(i=0; i<10; i++){ this.['wave'+i] = game.add.group(); this.['wave'+i].enableBody = true; this.['wave'+i].physicsBodyType = Phaser.Physics.ARCADE; } So I'm sure you got the idea but this (['wave'+i]) is the problem. Can someone help me with this? What is the correct way of writing it? I've been searching but maybe I'm not questioning this correctly. Thanks and sorry for the noob question!
-
hi, i'm searching to modify a variable with dat gui. Dat.gui appears on my screen and i have no error. The option "speed" appears also and i can modify the value but that do noting on my object. in fact i must access to this.weapon.fire() to update the result but i can't acess to it. What's the true syntax to access this function ? thanks for your help. _weapon = function(delay,posx,posy,speed,frequency,variance,angular,_flag,kill_with_world,special_color){ this.special_color=special_color this.kill_with_world=kill_with_world this.delay=delay this.posx=posx this.posy=posy this.flag_explode=false this.speed=speed this.angular=angular this.frequency=frequency this._flag=_flag this.variance=variance this.sound_pop=game.add.audio('pop') this._flag=true //canon Phaser.Sprite.call(this,game,this.posx,this.posy,'canon') this.anchor.setTo(.5,.5) this.angle=this.angular this.inputEnabled=true this.input.enableDrag(true) this.events.onDragStop.add(logic_position,this) this.events.onDragStart.add(show_grid_on_logic_position,this) this.input.enableSnap(40,40,true,true) game.physics.arcade.enable(this); if(this.special_color=="vrai"){ this.weapon=game.add.weapon(9,'bullet_color') }else{ this.weapon=game.add.weapon(9,'bullet') } if(this.kill_with_world=="faux"){ for (var i = 0; i < 9; i++) { this.weapon.bulletCollideWorldBounds=true this.weapon.bullets.children[i].body.bounce.setTo(1,1) } }else{ this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; } // Because our bullet is drawn facing up, we need to offset its rotation: this.weapon.bulletAngleOffset = 0; // The speed at which the bullet is fired this.weapon.bulletSpeed = this.speed; // Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms this.weapon.fireRate = this.frequency ; // Add a variance to the bullet angle by +- this value this.weapon.bulletAngleVariance = this.variance; // Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically this.weapon.trackSprite(this,0,0,true); game.time.events.add( this.delay,function(){this._flag=false},this ) } _weapon.prototype = Object.create(Phaser.Sprite.prototype) _weapon.prototype.constructor = _weapon _weapon.prototype.update = function(){ if(this._flag==false){ this.weapon.fire() } } //in state Level1// //var gui //var canon=[] canon[0]=new _weapon(800,200,800,900,2990,0,180,hero.flag_level_complete,"vrai","faux") gui=new dat.GUI() gui.add(canon[0],'fire') gui.add(canon[0],'speed',0,50) //
-
Hello ! I really want to read the source code of phaser, but I am not that professional. the namespace Phaser is declared inside a function, so I am wondering how we can access it outside it ? (function(){ var Phaser = Phaser || {}; }).call(this); Phaser.something = 5; //Reference error, Phaser is not declared what things I ca learn to read the source code perfectly and have this awesome experience ?
-
Hi, I want to get from database position of sprite and i am doing it with AJAX. In main file: var playerx = 0; var playery = 0; function start(){ $(document).ready(function(){ $.ajax({ type:"GET", /*Informacja o tym, że dane będą pobierane*/ url:"start.php", /*Informacja, o tym jaki plik będzie przy tym wykorzystywany*/ contentType:"application/json; charset=utf-8", /*Informacja o formacie transferu danych*/ dataType:'json', /*Informacja o formacie transferu danych*/ /*Działania wykonywane w przypadku sukcesu*/ success: function(json) { /*Funkcja zawiera parametr*/ /*Pętla typu for...in języka Javascript na danych w formacie JSON*/ for (var klucz in json) { var wiersz = json[klucz]; /*Kolejne przebiegi pętli wstawiają nowy klucz*/ var mapastart = wiersz[2]; playerx = wiersz[3]; playery = wiersz[4]; /*Ustalenie sposobu wyświetlania pobranych danych w bloku div*/ $("<span> mapastart: "+mapastart+"</span>") .appendTo('#wykaz') .append("<hr>") } TopDownGame.level = mapastart; console.log(playery); console.log(playerx); }, /*Działania wykonywane w przypadku błędu*/ error: function(blad) { alert( "Wystąpił błąd"); console.log(blad); /*Funkcja wyświetlająca informacje o ewentualnym błędzie w konsoli przeglądarki*/ } });}); }; An that i am adding sprite to the game: this.player = this.game.add.sprite(playerx, playery, 'player', 5);And in console after start i have that position what i have in database, but it looks like a words, and my sprite doesnt add to the game. Any idea what i can do with it? I will be gratefull.
-
Hello, is there any way to create a shader code(v&f) that comes from an ajax request like stored in a variable ?? regards
-
Hi, I'm trying to create a small javascript app which loads a JSON url and displays some html and divs based on the JSON data. It's got to be as small as possible so without using a frameworks like Phaser and jQuery. The javascript scope of variables is a bit tricky, and I'm having some trouble getting it to work properly. My question is, how can create the MyGame class with an imageShapes image and then reference that imageShapes in the onreadystatechange method of XMLHttpRequest? Here is my code:// namespacevar MyGame = MyGame || { gamedata: [], test123: 'This is a test value 123'};// game objectMyGame.start = function (jsonfilename) { // initialise image this.imageShapes = new Image(); // add gamedata and populate by loading json this.gamedata = []; this.test123 = 'This is a test value 123'; this.loadJSON( jsonfilename, this.onJSONsuccess, // <- function to call on successfully loaded JSON this.onJSONerror );}MyGame.start.prototype = { // load a JSON file loadJSON: function(path, success, error) { console.log('TESTING loadJSON : test123='+this.imageBackground); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if ((xhr.status === 200) || (xhr.status === 0)) { // status 0 = local file testing if (success) success(JSON.parse(xhr.responseText)); // <- how to reference back to "MyGame" ? } else { if (error) error(xhr); } } }; xhr.open("GET", path, true); xhr.send(); }, // handle load json events onJSONerror: function(xhr) { console.log('MyGame.js - onJSONerror error loading json file'); console.error(xhr); }, onJSONsuccess: function(data) { // load data from JSON this.gamedata = data; console.log('TESTING -- data.imgshapes='+data.imgshapes+' test123='+MyGame.test123+' imageShapes='+this.imageShapes); // this.imageShapes is also undefined MyGame.imageShapes.src = 'img/mybackground.png'; // <- problem is here, imageShapes is undefined..? }};var test = new MyGame.start('js/mydatafile.json');When I try the code above, it fails in onJSONsuccess. The imageShapes cannot reference and it is undefined, even though I defined it earlier in the MyGame class. This is the error: Uncaught TypeError: Cannot set property 'src' of undefined test123.js:58
- 5 replies
-
- javascript
- scope
-
(and 3 more)
Tagged with: