Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-15-2009, 04:44 AM
Ronnie Ronnie is offline
Registered User
Join Date: Jun 2008
Location: Nj, USA
Posts: 48
Ronnie is on a distinguished road
Send a message via AIM to Ronnie
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";
   
190;
   
140;
   
width 500;
   
height 440;
   
text "Catch That Greasy Chicken! !";
   
destroyonhide true;
   
canmaximize false;
   
canminimize false;
   
canresize false;
   
canclose true;

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

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

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



  }

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

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



 new 
GuiButtonCtrl"ron_button2" ) {
   
140;
   
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.random, ( ron_field.width 20 ) ) ;
  
ron_dot.random, ( 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.-= this.speed;
    break;

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

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

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

   }
  }

  
setTimer0.05 );
 }

function 
onKeyPressedkeycodekeynamescancode) {
  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;

  }
 } 
Reply With Quote
  #2  
Old 07-15-2009, 05:49 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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.0temp.4temp.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.+= vecx(this.direction) * this.speed;
  
ron_ply.+= 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
    
setTimer0.05 );
  }

Following your code base, that above example would continue timing out every 0.05 seconds while the gamestatus was on.
__________________
Quote:
Reply With Quote
  #3  
Old 07-15-2009, 06:24 AM
Ronnie Ronnie is offline
Registered User
Join Date: Jun 2008
Location: Nj, USA
Posts: 48
Ronnie is on a distinguished road
Send a message via AIM to Ronnie
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.0temp.4temp.i++) {
    if (
keydown(temp.i)) {
     
this.direction temp.i;
     break;
    }
  }

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


  
setTimer0.05 );
 }

Reply With Quote
  #4  
Old 07-15-2009, 06:58 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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";
    
287;
    
241;

    new 
GuiButtonCtrl("GameGUI_Button") {
      
profile GuiBlueButtonProfile;
      
text "Exit";
      
width 80;
      
119;
      
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.0temp.4temp.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.+= vecx(this.direction) * this.speed;
      
GameGUI_Button.+= vecy(this.direction) * this.speed;
    }
    
// Freezeplayer to prevent graal movement
    
freezeplayer(0.05);
    
// Continue game loop
    
setTimer(0.05);
  }

__________________
Quote:

Last edited by fowlplay4; 07-15-2009 at 07:09 AM..
Reply With Quote
  #5  
Old 07-16-2009, 01:45 AM
Ronnie Ronnie is offline
Registered User
Join Date: Jun 2008
Location: Nj, USA
Posts: 48
Ronnie is on a distinguished road
Send a message via AIM to Ronnie
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!.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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