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 jmpaul

So on my end state I want to have the user input their initials. I have setup an array that has all the alphabet, triggers to select a letter and font_draw, but I cant seem but I get an error saying my function is not defined. Any ideas?

userInitials: function(){
			io_clear();
			a = 0
			l[0] = " "
			l[1] = "a"
			l[2] = 'b'
			l[3] = 'c'
			l[4] = 'd'
			l[5] = 'e'
			l[6] = 'f'
			l[7] = 'g'
			l[8] = 'h'
			l[9] = 'i'
			l[10] = 'j'
			l[11] = 'k'
			l[12] = 'l'
			l[13] = 'm'
			l[14] = 'n'
			l[15] = 'o'
			l[16] = 'p'
			l[17] = 'q'
			l[18] = 'r'
			l[19] = 's'
			l[20] = 't'
			l[21] = 'u'
			l[22] = 'v'
			l[23] = 'w'
			l[24] = 'x'
			l[25] = 'y'
			l[26] = 'z'
			str = ""

			if(ig.input.pressed('up')){				
				if (a>26){
					a+= 1;
				}else{
					a = 0;
				}
			}

			if(ig.input.pressed('down')){
				if (a<0){
					a -= 1;
				}else{
					a = 26;
				}
			}

			this.font_draw(x,y,str+'['+1[a]+'] ');

			if(ig.input.pressed('space')){
				str += l[a];
			}
		},

draw:function(){
	if(this.gameOver){
this.font.draw(userInitials, ig.system.width/2, 95, ig.Font.ALIGN.CENTER);
}

1 decade ago by jmpaul

Just fixed the function call. Forgot to call this.userInitials.

1 decade ago by jmpaul

With this setup seems I have to put something in the update function to register the keys but I have no idea what to put there.

1 decade ago by Joncom

If you're wondering how to setup key bindings so that you can detect when certain keys have been pressed, this is what you're looking for.

// put this in ig.game.init
ig.input.bind( ig.KEY.A, 'a' );
ig.input.bind( ig.KEY.A, 'b' );
ig.input.bind( ig.KEY.A, 'c' );

// put this in ig.game.update or your entity update function
if( ig.input.pressed('a') ) {
    // user pressed 'a'
}

1 decade ago by jmpaul

Thanks Joncom. Im storing the users initials in an array and with this method the update function is called every frame so the array is over written every frame.


userInitials: function(){
			user_id = [];
			if(ig.input.pressed('a')){
				user_id.push('a');
			}

			console.log(user_id);
		},

update: function() {
 if(this.gameOver){
  this.userInitials();
}
}

1 decade ago by lazer

Is that intentional? If not, why not store user_id in the init function so that it does not get overwritten every frame? (sorry if I'm totally misunderstanding the end goal here)

1 decade ago by mLautz

I am also a little confused by this. The second function you posted only seems to write to the console the current keys being held down during each update call.

The function in your initial post makes it seem as though you want the user to enter their initials with the arrow keys and not by pressing the corresponding letter keys.

Based on your original post I would try something like the following:

//in your init function
    initialsStr = "";
    charIndex = 0;

    ...

    ig.input.bind(ig.KEY.UP_ARROW, 'up');
    ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
    ig.input.bing(ig.KEY.SPACE, 'space');

//In your draw function
if(this.gameOver){
    this.userInitials();
}

//the user initials function
userInitials: function(){
    if(ig.input.pressed('up')){
         if(this.charIndex >= 25){
             this.charIndex = 0;
         }else{
             this.charIndex++;
         }
    }

    if(ig.input.pressed('down')){
         if(this.charIndex <= 0){
             this.charIndex = 25;
         }else{
             this.charIndex--;
         }
    }

    if(ig.input.pressed('space')){
        //convert the index value (0-25) to a capital letter. 'A' = 65
        this.initialsStr += String.fromCharCode(65 + this.charIndex);
    }
    
    this.font.draw(this.initialsStr, ig.system.width/2, 95, ig.Font.ALIGN.CENTER);
}

Writing it this way, should prevent your initials string from being reset every frame. And would also handle the up/down/space inputs to match the style that you seemed to be aiming for in your first post.

The String.fromCharCode() is something I found on StackOverflow and should convert your integer value 0-25 to the capital corresponding capital letter.

I did not include any checks that would prevent the initials string from going beyond a given size limit. And I have not been able to test this exact code, but hopefully the concept may be of some help.
Page 1 of 1
« first « previous next › last »