WiLD11 Posted January 4, 2016 Share Posted January 4, 2016 So I have been working on this tycoon game for a few days now, (you can try what I have so far here)http://www.newgrounds.com/projects/games/935963/preview As a next feature I was going to add a money system, where each tile would cost $15 to place down Here is the code I'm using to place a tile downif (game.input.mousePointer.isDown) {if(!outarange){ if (sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile) { mtween.x = this.input.activePointer.worldX; mtween.y = this.input.activePointer.worldY-50; mtween.visible = true; money -=15; sandbox.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y)); } }}If you tried the demo, you might have realised that the money value goes down every frame while you're holding down the click button. How would I do it so it only gets decreased by 15 everytime a new tile is placed. Thanks for the interest Link to comment Share on other sites More sharing options...
aceofpack Posted January 4, 2016 Share Posted January 4, 2016 Your check here: if (sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile) So if this works, it should prevent the money subtraction from taking place. Unless I'm reading it wrong? Is currentTile the currently selected tile from the bottom bar? Link to comment Share on other sites More sharing options...
WiLD11 Posted January 4, 2016 Author Share Posted January 4, 2016 Your check here: if (sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile) So if this works, it should prevent the money subtraction from taking place. Unless I'm reading it wrong? Is currentTile the currently selected tile from the bottom bar? Sorry, should have stated the currentTile variable, currentTile = sandbox.getTile(0,0); sandbox is the map itselfsandbox = this.add.tilemap('sandbox', 32, 32);The thing is that it gets taken away each frame while the click button is down. If only there was a way it could happen every time a tile is placed Link to comment Share on other sites More sharing options...
aceofpack Posted January 4, 2016 Share Posted January 4, 2016 Well the logic would be to check if that tile is already there, if it is do nothing, otherwise do 'something' which includes taking away from the money. If sandbox is keeping a record of what types of tiles are placed where, when a click happens in any particular grid square, check if that tile already exists there. If it does, yeah, do nothing. This logic can also be used when removing a tile, if they try to remove something that is blank, do nothing - otherwise it might incur a cost and you update that new tile. Sorry for lack of code, I haven't played around with tilemaps as yet but I'm sure that you can save a property in there with a reference to the type of tile that exists at those co-ordinates... Link to comment Share on other sites More sharing options...
WiLD11 Posted January 4, 2016 Author Share Posted January 4, 2016 Well the logic would be to check if that tile is already there, if it is do nothing, otherwise do 'something' which includes taking away from the money. If sandbox is keeping a record of what types of tiles are placed where, when a click happens in any particular grid square, check if that tile already exists there. If it does, yeah, do nothing. This logic can also be used when removing a tile, if they try to remove something that is blank, do nothing - otherwise it might incur a cost and you update that new tile. Sorry for lack of code, I haven't played around with tilemaps as yet but I'm sure that you can save a property in there with a reference to the type of tile that exists at those co-ordinates... Isn't that what the code does tough? It checks if the certain tile is not the same, and then if it isn't it places the tile. Link to comment Share on other sites More sharing options...
aceofpack Posted January 4, 2016 Share Posted January 4, 2016 That's what I initially thought but double check if the 'if' statement is correct, i.e, you're checking apples vs apples. If it is correct, there is no way that code should be executed. console.log: sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) and this: currentTile to make sure you are comparing apples with apples Link to comment Share on other sites More sharing options...
aceofpack Posted January 4, 2016 Share Posted January 4, 2016 Oh sorry and in javascript comparing objects is not as it seems:http://stackoverflow.com/questions/1068834/object-comparison-in-javascript can you access just a property of the objects for comparison? Link to comment Share on other sites More sharing options...
WiLD11 Posted January 4, 2016 Author Share Posted January 4, 2016 Oh sorry and in javascript comparing objects is not as it seems:http://stackoverflow.com/questions/1068834/object-comparison-in-javascript can you access just a property of the objects for comparison? When values are printed out, they are equal, and empty block returns null but a rail returns object. I'm not really sure what you ment Link to comment Share on other sites More sharing options...
WiLD11 Posted January 4, 2016 Author Share Posted January 4, 2016 -Edit- that didn't work well Link to comment Share on other sites More sharing options...
aceofpack Posted January 5, 2016 Share Posted January 5, 2016 Sorry, it's difficult without being able to run the code and explore it. I'm assuming the below is where you're comparing the tile from the currently selected tile co-ordinates against the tile that is currently 'on the paintbrush': if (sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile) You can't compare objects with the '!=' or '==' in javascript. You can only compare value of an object this way (some examples below). Could you show me what the output is for 'sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y))' and 'currentTile'. I'm saying that I think 'sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y))' returns an object or currentTile does - so the comparison won't work as intended. Check this example out for trying to compare objects: https://jsfiddle.net/uywjmpuf/ <- objects are obv the same but evaluates falseNow this one for properties of an object: https://jsfiddle.net/6xzs8guh/ <- obj.value is the same, that's ok I think this is where the problem is. Link to comment Share on other sites More sharing options...
aceofpack Posted January 5, 2016 Share Posted January 5, 2016 When one is null, it should work, but then the next time, when they both return objects won't work. Try something like this - again it's hard to know without running the code but can you see the checks here?if (game.input.mousePointer.isDown){ if(!outarange){ // Tile at grid position var tileAtMarker = sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)); if (tileAtMarker != null){ if(tileAtMarker.someProperty != currentTile.someProperty) { //Replace someProperty with something they both would have in common //We've established they are different, so add a tile addTile(); } } else { //Tile at marker is null so add a tile addTile(); }}function addTile() { mtween.x = this.input.activePointer.worldX; mtween.y = this.input.activePointer.worldY-50; mtween.visible = true; money -=15; sandbox.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y));} Link to comment Share on other sites More sharing options...
WiLD11 Posted January 5, 2016 Author Share Posted January 5, 2016 When one is null, it should work, but then the next time, when they both return objects won't work. Try something like this - again it's hard to know without running the code but can you see the checks here?if (game.input.mousePointer.isDown){ if(!outarange){ // Tile at grid position var tileAtMarker = sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)); if (tileAtMarker != null){ if(tileAtMarker.someProperty != currentTile.someProperty) { //Replace someProperty with something they both would have in common //We've established they are different, so add a tile addTile(); } } else { //Tile at marker is null so add a tile addTile(); }}function addTile() { mtween.x = this.input.activePointer.worldX; mtween.y = this.input.activePointer.worldY-50; mtween.visible = true; money -=15; sandbox.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y));}That kind of worked, the tiles now cost money to place, and it goes -15 per click, however when i try erase (just replaces the tile) it says currentTile is null <3 Link to comment Share on other sites More sharing options...
chg Posted January 5, 2016 Share Posted January 5, 2016 You can't compare objects with the '!=' or '==' in javascript. Check this example out for trying to compare objects: https://jsfiddle.net/uywjmpuf/ <- objects are obv the same but evaluates falseThose objects are not the same, their properties are. Javascript does the comparison the way many other languages do, evaluating the object comparison as true when both sides represent the same instance of an object. This means you can store a sprite in a variable as the selected sprite and then later compare that to a selected sprite - it will return true not when the properties are the same but only when it is actually that sprite. Here is a trivial example (modified from yours) showing this: https://jsfiddle.net/eokx65ou/ Link to comment Share on other sites More sharing options...
aceofpack Posted January 5, 2016 Share Posted January 5, 2016 Good point chg! WiLD11 you can just check for a null currentTile:if(game.input.mousePointer.isDown) { if(!outarange){ // Tile at grid position var tileAtMarker = sandbox.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)); if(tileAtMarker != null) { if(currentTile != null) { if(tileAtMarker.someProperty != currentTile.someProperty) { //Replace someProperty with something they both would have in commone //We've established they are different, so add a tile addTile(15); // <-- you could pass a cost here } } else { //Current tile is null but marker isn't addTile(5); // <-- you could pass a diff cost here } } else { //Tile at marker is null so add a tile only if currentTile is not null if(currentTile != null) { addTile(15); } }}function addTile(cost) { mtween.x = this.input.activePointer.worldX; mtween.y = this.input.activePointer.worldY-50; mtween.visible = true; money -=cost; sandbox.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y));} Link to comment Share on other sites More sharing options...
WiLD11 Posted January 5, 2016 Author Share Posted January 5, 2016 Yeah, that makes sense. I never really knew about all these comparison things javascript has. It works nicely now.Thanks for dealing with my stupidness aceofpack 1 Link to comment Share on other sites More sharing options...
Recommended Posts