Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Odd Scripting error. (https://forums.graalonline.com/forums/showthread.php?t=86904)

Ronnie 07-15-2009 04:44 AM

Odd Scripting error.
 
I am making a kinda version of snake, but instead of growing longer I have the object move faster , so this is where the problem comes in, on the timeout i have it move the gui control based on the direction, but if the direction is going ether right or down it doesn't work , and I really cant find out why. Thanks in advance.

PHP Code:

function onPlayerTouchsme() {
  new 
GuiWindowCtrl( "ron_game" ) {
   
useownprofile = true;
   
profile = "teststyle";
   
x = 190;
   
y = 140;
   
width = 500;
   
height = 440;
   
text = "Catch That Greasy Chicken! !";
   
destroyonhide = true;
   
canmaximize = false;
   
canminimize = false;
   
canresize = false;
   
canclose = true;

  new 
GuiScrollCtrl( "ron_field" ) {
  
x = 24;
  
y = 35;
  
width = 450;
  
height = 300;
  
hScrollBar = "dynamic";
  
vScrollBar = "alwaysOff";

  new 
GuiShowImgCtrl( "ron_dot" ) {
  
x = random(1,248);
  
y = random(1,190);
  
width = 60;
  
height = 60;
  
ani = "cry";
  
actor.head = "head180.png";
  
actor.body = "lgarmor-body100.png";
  }

  new 
GuiShowImgCtrl( "ron_plr" ) {
   
x = ron_field.width/2;
   
y = ron_field.height/2;
   
width = 60;
   
height = 60;
   
ani = "walk";
   
actor.head = player.headimg;
   
actor.body = player.bodyimg;
   }



  }

   new 
GuiButtonCtrl( "ron_button0" ) {
   
x = 30;
   
y = 350;
   
width = 215;
   
height = 30;
   
text = "Start Game!";
   }

   new 
GuiButtonCtrl( "ron_button1" ) {
   
x = 255;
   
y = 350;
   
width = 215;
   
height = 30;
   
text = "End Game!";
   }



 new 
GuiButtonCtrl( "ron_button2" ) {
   
x = 140;
   
y = 390;
   
width = 215;
   
height = 30;
   
text = "Controls & Help!";
   }


 }
}

function 
ron_button0.onAction() {
 if ( 
this.gamestatus != "on" ) {
  
//disabledefmovement();
  
this.gamestatus = "on";
  
this.level = 1;
  
onTimeout();
  
ron_dot.x = random( 1 , ( ron_field.width - 20 ) ) ;
  
ron_dot.y = random( 1 , ( ron_field.height - 20) ) ;
  
ron_game.pushtoback();
  }
 }

function 
onTimeout() {
  
this.speed = ( ( this.level * .25 ) + .15 );

/*Checking for hitting of the dot.
  if ( ( (ron_plr.x - ron_dot.x) < 1 ) || ( (ron_plr.x + ron_dot.x) < 1 ) ) {
   if ( (ron_plr.x - ron_dot.x) < 1) || ( (ron_plr.x + ron_dot.x) < 1) ) ) {
   this.score += ( this.level * 2.5 );
   this.level += 1;
   ron_dot.x = int[random( 1 , 248 )];
   ron_dot.y = int[random( 1 , 190 )];
   }
  }
 */

  
ron_game.text = "Catch that Greasy Chicken!               Score : " @ this.score;
  if ( 
ron_game.visible != true ) {
   
this.gamestatus = "off";
   }

  if ( 
this.gamestatus == "on" ) {
   switch ( 
this.direction ) {

    case 
1:
    
ron_plr.dir = 1;
    
ron_plr.x -= this.speed;
    break;

    case 
3:
    
ron_plr.dir = 2;
    
ron_plr.x += this.speed;
    break;

    case 
4:
    
ron_plr.dir = 3;
    
ron_plr.y += this.speed;
    break;

    case 
2:
    
ron_plr.dir = 0;
    
ron_plr.y -= this.speed;
    break;

   }
  }

  
setTimer( 0.05 );
 }

function 
onKeyPressed( keycode, keyname, scancode) {
  switch( 
keycode ) {
  case 
"37":
  
this.direction = 1;
  break;

  case 
"38":
  
this.direction = 2;
  break;

  case 
"39":
  
this.direction = 4;
  break;

  case 
"40":
  
this.direction = 3;
  break;

  }
 } 


fowlplay4 07-15-2009 05:49 AM

Remember, Graal's directions use the values, 0, 1, 2, 3 and has built-in functions to make tasks working with them easy.

A good way to get the direction to move would have to be adding key checking in your Timeout loop like so.

PHP Code:

//#CLIENTSIDE
// Your GUI Code..

function onTimeout() {
  
// Loop through Arrow/Direction Keys
  
for (temp.i = 0; temp.i < 4; temp.i++) {
    
// Check if direction key is pressed
    
if (keydown(temp.i)) {
      
// Record the direction pressed and exit the loop.
      
this.direction = temp.i;
      break;
    }
  }
  
// Move your control based on the direction vector, and multiply that by the speed.
  
ron_plr.x += vecx(this.direction) * this.speed;
  
ron_ply.y += vecy(this.direction) * this.speed;
  
// Other Code..
}

// Other Code.. 

Now to explain vecx, and vecy. The return -1, 0 or 1 depending on the direction passed to them..

PHP Code:

echo(vecx(0)); // would display 0
echo(vecx(1)); // would display -1
echo(vecx(2)); // would display 0
echo(vecx(3)); // would display 1 

The reason 0 and 2 when passed into vecx is because it basically simulates as if you were moving 1 space to the direction specified. So if you were moving straight up or down, only your Y axis value would change. Vice-versa for vecy.

Also another good habit to get into is only loop when you need to!

PHP Code:

function onTimeout() {
  if ( 
ron_game.visible != true ) {
     
this.gamestatus = "off";
  }
  else if (
this.gamestatus == "on") {
    
this.speed = ( ( this.level * .25 ) + .15 );
    
ron_game.text = "Catch that Greasy Chicken!               Score : " @ this.score;
    
// Movement Stuff here
    // Other Game related code..
    // Continue Looping
    
setTimer( 0.05 );
  }
} 

Following your code base, that above example would continue timing out every 0.05 seconds while the gamestatus was on.

Ronnie 07-15-2009 06:24 AM

Well, after your post i rescripted the timeout , which ur way also made it much more effiecient thank you, but it still does not move right or down ;/


PHP Code:

function onTimeout() {
  
this.speed = ( ( this.level * .25 ) + .15 );

/*Checking for hitting of the dot.
  if ( ( (ron_plr.x - ron_dot.x) < 1 ) || ( (ron_plr.x + ron_dot.x) < 1 ) ) {
   if ( (ron_plr.x - ron_dot.x) < 1) || ( (ron_plr.x + ron_dot.x) < 1) ) ) {
   this.score += ( this.level * 2.5 );
   this.level += 1;
   ron_dot.x = int[random( 1 , 248 )];
   ron_dot.y = int[random( 1 , 190 )];
   }
  }
 */

  
ron_game.text = "Catch that Greasy Chicken!               Score : " @ this.score;
  if ( 
ron_game.visible != true ) {
   
this.gamestatus = "off";
   }

  if ( 
this.gamestatus == "on" ) {
   for (
temp.i = 0; temp.i < 4; temp.i++) {
    if (
keydown(temp.i)) {
     
this.direction = temp.i;
     break;
    }
  }

  
ron_plr.x += vecx(this.direction) * this.speed;
  
ron_plr.y += vecy(this.direction) * this.speed;
  
ron_plr.dir = this.direction;


  
setTimer( 0.05 );
 }
} 


fowlplay4 07-15-2009 06:58 AM

I just tested my examples and they worked fine, also remember you're working with pixels not Graal x, y, coordinates. So try increasing your speed above 1.

Try.. this.speed = this.lvl * 1;

That assumes that you're setting this.lvl to 1 though.

Also just learned that you should use keydownglobal() function instead of keydown()

Here is my example..

PHP Code:

//#CLIENTSIDE
function onCreated() {
  new 
GuiWindowCtrl("GameGUI") {
    
profile = GuiBlueWindowProfile;
    
clientrelative = true;
    
clientextent = "320,240";

    
canmove = true;
    
canresize = false;
    
closequery = false;
    
destroyonhide = false;
    
text = "Game - Use Arrow Keys to Move Button";
    
x = 287;
    
y = 241;

    new 
GuiButtonCtrl("GameGUI_Button") {
      
profile = GuiBlueButtonProfile;
      
text = "Exit";
      
width = 80;
      
x = 119;
      
y = 108;
    }
  }
  
setTimer(0.05);
}

function 
GameGUI_Button.onAction() {
  
// Button "Fun Example" has been pressed
  
GameGUI.destroy();
}

function 
onTimeout() {
  if (
GameGUI.visible) {
    
this.speed = 2;
    
// Loop through Arrow/Direction Keys
    
for (temp.i = 0; temp.i < 4; temp.i++) {
      
// Check if direction key is pressed
      
if (keydownglobal(temp.i)) {
        
// Record the direction pressed and exit the loop.
        
this.direction = temp.i;
        
temp.pressed = true;
        break;
      }
    }
    
// Check if a Direction key was pressed.
    
if (temp.pressed) {
      
// Move Button
      
GameGUI_Button.x += vecx(this.direction) * this.speed;
      
GameGUI_Button.y += vecy(this.direction) * this.speed;
    }
    
// Freezeplayer to prevent graal movement
    
freezeplayer(0.05);
    
// Continue game loop
    
setTimer(0.05);
  }
} 


Ronnie 07-16-2009 01:45 AM

Ah thanks for all the help fowlplay, I found my script error, i was using this.level which is obviously a reserved variable so for my this.speed I was getting 0 so I was not moving those 2 directions :P, once again thanks alot man!.


All times are GMT +2. The time now is 04:47 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.