Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by ShawnSwander

So based on the data from my map when you right click I want to show the background tile the player clicked on blown up. The scaling script works fine I can't assign a picture without a huge switch statement though. is there a shorter way?
I don't know how to assign an animation from settings or with a varriable name in the assignment.
Here's my attempt
if( ig.input.state('rightclick')){
            
            tiles = ig.game.getMapByName('hexes');
            tilex = parseInt(ig.input.mouse.x / HEX_SIZE);
            tiley = parseInt(ig.input.mouse.y / HEX_SIZE);
            char='abcdefghijklmnopqrstuvwxyz'[tiles.data[tilex][tiley]]
            settings = "{this.currentAnim = this.anims."+char+";}";
            this.messagebox = settings;
            ig.game.spawnEntity(EntityHexzoom, this.pos.x-165,this.pos.y-165,settings);
            var hexzoom = ig.game.getEntitiesByType( EntityHexzoom );
            //hexzoom.currentAnim = "hexzoom.anims."+char;
         }

my init (shortened)
      init: function( x, y, settings ) {
         // Add the animations
         this.addAnim( 'a', 0.1, [0] );
         this.addAnim( 'b', 0.1, [1] );
         this.addAnim( 'c', 0.1, [2] );
         this.addAnim( 'd', 0.1, [3] );
         this.addAnim( 'e', 0.1, [4] );
        ...

 this.currentAnim = this.anims.a;
         this.parent( x, y, settings );
      },

1 decade ago by Joncom

Would you mind explaining this line to me?
char='abcdefghijklmnopqrstuvwxyz'[tiles.data[tilex][tiley]]

So instead of making a switch(char) {}, you'd like to use char as part of the variable name?

This can be done... let&039;s say that #char=='a', or b, or c, etc. and was stored by this entity. Instead of having to call this.a or this.b, you can simply call this[char]

Hope that helps.

1 decade ago by ShawnSwander

Quote from Joncom
Would you mind explaining this line to me?
char='abcdefghijklmnopqrstuvwxyz'[tiles.data[tilex][tiley]]



In hindsight it looks terrible.

String from char code was supposed to pull a letter from that string to correspond to a frame of animation...

Here is the goal I want and I'm still working on it.

You right click on a tile and the screen zooms in on that tile.
Right click again zoom out back to normal view

I'm accomplishing this by spawning an entity with the same image as the tile the player clicks on.
the problems I'm running into are that my kill function isn't working and I'm having trouble getting an image from a string.

I have no problem getting the data for the tile ... lets assume the data at the tile I click is "13"
I need to be able to take that 13 and set my zoomed in entity to the same picture. The best way I know of is a switch which is fine but I may have hundreds of tiles.

I'm noticing I'm struggling with toggled events such as this.

1 decade ago by Joncom

Sounds like a better approach to a switch with hundreds of lines would be to create a JavaScript object:
var tileInformation = new Object();

Then for each case of what would have been a switch, you do this:
tileInformation[case]

Meaning if you could have one tile "a", another "b", and other "c", you could store it like this:
// Allow case 'a' to have multiple properties.
tileInformation['a'] = new Object();

// Set image info.
tileInformation['a'].img = 'media/tile-a.png';

// Set some other info.
tileInformation['a'].x = 256;
tileInformation['a'].y = 224;

// Repeat for as many cases as you need to store tile information.
tileInformation['b'] = new Object();
tileInformation['b'].img = 'media/tile-a.png'; // ... and so on...

You could assign these values using a for-loop.

Then finally when you originally were going to have a switch statement, you just throw your case var into the object to grab the info you need.
var everythingYouNeedToKnow = tileInformation[case];
Page 1 of 1
« first « previous next › last »