snd_play on key press?

Posted By: MrCode

snd_play on key press? - 04/21/07 00:32

This code I THOUGHT would play a sound when I hit the "j" key.

Instead, it plays at startup and if i do press the "j" key, the engine aborts with "acknex.exe has encountered a problem..."!

Is this not the right code for it?

Code:

on_j= smb_jump();

...

function smb_jump()
{
snd_play (jump_snd,100,0);
}



I already tried a while() loop, but all that does is play the sound every frame cycle, and that gets really annoying.

So, can anyone help?
Posted By: qwerty823

Re: snd_play on key press? - 04/21/07 00:51

Quote:

This code I THOUGHT would play a sound when I hit the "j" key.

Instead, it plays at startup and if i do press the "j" key, the engine aborts with "acknex.exe has encountered a problem..."!

Is this not the right code for it?

Code:

on_j= smb_jump();

...

function smb_jump()
{
snd_play (jump_snd,100,0);
}



I already tried a while() loop, but all that does is play the sound every frame cycle, and that gets really annoying.

So, can anyone help?




Common mistake.

on_j = smb_jump(); // This *runs* the function smb_jump, and assigns its return to on_j

on_j = smb_jump; // This assigns the function itself to on_j


You want the second one.
Posted By: MrCode

Re: snd_play on key press? - 04/21/07 02:29

Parameter unknown smb_jump. is there something else I'm missing?

Here's the whole Code:

var video_mode= 8;
var video_screen= 1;
var video_depth= 32;

var walk_speed;
var breath_speed;
var jump_speed;

string anim_wmb= <JumpDude.wmb>;

string keys_str= "Use the arrow keys to move around.";
string keys2_str= "Press the J key to jump.";
string keys3_str= "Press I to see these instructions again.";

sound jump_snd= <smb_jumpsmall.wav>;

bmap intro_pcx= "jdudeintro.pcx";

function main()
{
level_load (anim_wmb);
wait(3);
camera.x= -48;
camera.y= 334;
camera.z= 149;
camera.pan= 281;
camera.tilt= -48;
camera.roll= 0;
pan_fade();
txt_show();
show_again();
on_j= smb_jump;
}

action char_walk
{
my.fat= on;
my.narrow= on;
c_setminmax(my);
while(1)
{
if(key_cuu== on)
{
c_move(my,vector(0,6 * time_step,0),nullvector,glide);
ent_animate(my,"walk",walk_speed,anm_cycle);
walk_speed+= 7 * time_step;
}
else
{
ent_animate(my,"stand",breath_speed,anm_cycle);
breath_speed+= 3 * time_step;
}
if(key_cud== on)
{
c_move(my,vector(0,-6 * time_step,0),nullvector,glide);
ent_animate(my,"walk",walk_speed,anm_cycle);
walk_speed+= -7 * time_step;
}
if(key_cul== on)
{
my.pan+= 10 * time_step;
}
if(key_cur== on)
{
my.pan-= 10 * time_step;
}
if(key_j== on)
{
c_move(my,vector(0,0,15 * time_step),nullvector,glide);
ent_animate(my,"jump",jump_speed,anm_cycle);
jump_speed+= 14 * time_step;
}
else
{
c_move(my,vector(0,0,-15 * time_step),nullvector,glide);
}
wait(1);
}
}

panel intro
{
pos_x= 0;
pos_y= 0;
bmap= intro_pcx;
flags= transparent, overlay, visible;
alpha= 100;
}

font big_fnt= "Times New Roman",1,20;

text keys_txt
{
pos_x= 384;
pos_y= 550;
font= big_fnt;
string= keys_str;
}

function pan_fade()
{
wait(-2);
while(1)
{
intro.alpha-= 3 * time_step;
wait(1);
}
}

function txt_show()
{
wait(-3);
keys_txt.visible= on;
wait(-3);
keys_txt.string= keys2_str;
wait(-3);
keys_txt.string= keys3_str;
wait(-3);
keys_txt.visible= off;
}

function show_again()
{
while(1)
{
if(key_i== on)
{
keys_txt.visible= on;
wait(1);
keys_txt.string= keys_str;
wait(-3);
keys_txt.string= keys2_str;
wait(-3);
keys_txt.string= keys3_str;
wait(-3);
keys_txt.visible= off;
}
wait(1);
}
}

function smb_jump()
{
snd_play(jump_snd,100,0);
}


Posted By: zazang

Re: snd_play on key press? - 04/21/07 02:46

anywhere above the main function,just define a function prototype liek this :-

function smb_jump();

that shud fix it !
Posted By: MrCode

Re: snd_play on key press? - 04/21/07 03:15

Duh! I should have known! it was trying to call the function that doesn't exist until after that line!
Posted By: zazang

Re: snd_play on key press? - 04/21/07 03:19

I make worse mistakes than this
© 2024 lite-C Forums