Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Problem with move (https://forums.graalonline.com/forums/showthread.php?t=83147)

Codein 12-14-2008 12:07 AM

Problem with move
 
For some reason, when the NPC reaches directly behind the player, the position of the NPC resets to the start position. This shouldn't happen. Can anybody help me?

PHP Code:

function onCreated() {

  
setshape(13232);

  
this.speed 12;
  
  
//format: idle animation, walking animation
  
this.ganis = {"idle""walk"};
  
  
showcharacter();
  
setCharAni("idle"null);
  
  
setTimer(0.1);
}

function 
onPlayerChats() {
  switch (
player.chat) {
    case 
":destroy":
      
destroy();
      break;
    
    case 
":follow":
      
this.following true;
      
setTimer(0.1);
      break;
    
    case 
":stop following":
      
this.following false;
      
setTimer(0.1);
      break;
  }
}

function 
onTimeout() {
  
this.nick this.attr[5] @ "'s Pet";
  
  if (
this.following == true) {
    
    
//get the coordinates that are directly behind the owner
    
for (plplayers) {
      if (
pl.account == this.attr[5]) {
        
temp.newxy = {
                      
pl.0.5 - (vecx(pl.dir) * 3),
                      
pl.- (vecy(pl.dir) * 3)
                      };
                    
        
//pl.chat = vecx(player.dir) SPC vecy(player.dir) SPC player.dir;
        
break;
      }
    }
    
    
//work out delta_x and delta_y for move
    
temp.dxy = {
                
temp.newxy[0] - this.x,
                
temp.newxy[1] - this.y
               
};
    
    
    
this.dir getdir(temp.dxy[0], temp.dxy[1]);
    
     
//set the animations
    
if (this.== temp.newxy[0] && temp.== this.newxy[1]) {
       if (
this.ani != this.ganis[0]) {
        
setCharAni( @ this.ganis[0], null);
       } 
     }
  
    else {
      if (
this.ani != this.ganis[1]) {
        
setCharAni( @ this.ganis[1], null);
      }
    }
    
    
//work out the distance and time to cover that distance
    
temp.dist getDistance(temp.dxy[0], temp.dxy[1]);
    
temp.time temp.dist/this.speed;
    
    
move(this.dxy[0], temp.dxy[1], temp.time4);
    
    
    if (
temp.time 0.1temp.time 0.1;
    
    
setTimer(temp.time);
  }
}

function 
getDistance(temp.xtemp.y) {
  
temp.dist = (temp.temp.2) ^ 0.5;
  return 
temp.dist;



xXziroXx 12-14-2008 12:19 AM

It might be due to the block check - if move() is checking for blocking tiles (4), and it encounters one, it resets back to the position where it was at before using move().

Codein 12-14-2008 12:21 AM

Thank you so much.

thatdwarf 12-16-2008 08:36 AM

Ok this thread title summarizes my problem, so I will post here too (sorry for the thread steal codein <3)

PHP Code:

function onCreated() {
  
this.image "block.png";
  
setTimer(2);
  
onMovementFinished();
}
function 
onMovementFinished() {
  if(!
onwall(this.imgwidth(this.image), this.imgheight(this.image))) {
    
temp.movex sin(this.angle) * this.speed;
    
temp.movey cos(this.angle) * this.speed;
    
move(movexmovey.18);
  }
}

function 
onTimeout() {
  
destroy();


for the record, this.angle and this.speed are being set in a weapon NPC calling this class
this.angle = getangle(vecx(player.dir), vecy(player.dir));
this.speed = .5;

the move angle goes into an extremely different angle when fired

Suggestions?

Tigairius 12-16-2008 09:05 AM

Quote:

Originally Posted by thatdwarf (Post 1449821)
PHP Code:

function onCreated() {
  
this.image "block.png";
  
setTimer(2);
  
onMovementFinished();
}
function 
onMovementFinished() {
  if(!
onwall(this.imgwidth(this.image), this.imgheight(this.image))) {
    
temp.movex sin(this.angle) * this.speed;
    
temp.movey cos(this.angle) * this.speed;
    
move(movexmovey.18);
  }
}

function 
onTimeout() {
  
destroy();



PHP Code:

function onCreated() {
  
this.image "block.png";
  
setTimer(2);
  
onMovementFinished();
}
function 
onMovementFinished() {
  if(!
onwall(this.imgwidth(this.image), this.imgheight(this.image))) {
    
temp.movex sin(this.angle) * this.speed;
    
temp.movey cos(this.angle) * this.speed;
    
move(this.movexthis.movey.18);
  }
}

function 
onTimeout() {
  
destroy();



thatdwarf 12-16-2008 07:34 PM

hmm, still not working

here is the control weapon:

PHP Code:

function onActionServerSide(temp.cmdtemp.otemp.dxtemp.dy) {
  if(
cmd == "fire" && temp.== "thatdwarf") {
    
temp.op findplayer(o);
    
temp.obj putnpc2(op.xop.y"");
    
temp.obj.ndx dx;
    
temp.obj.ndy dy;
    
temp.obj.angle getangle(vecx(op.dir), vecy(op.dir));
    
temp.obj.join("lloydprojectile");
  }
}

//#CLIENTSIDE
function onCreated() {
  
this.on false;
}

function 
onWeaponFired() {
  
this.on = !this.on;
  
player.chat this.on;
}

function 
onMouseDown(code) {
  if(
code == "left" && this.on) {
    
triggerServer("gui"this.name"fire"player.accountmousexmousey);
  }


And I also tried the method you suggested in my class Tig, so maybe it's something with the weapon?

The image shoots way off to an extreme corner of the level, then disappears

Tigairius 12-16-2008 09:00 PM

not sure, but try using different variables instead of angle & speed, like this.pangle and this.pspeed. Otherwise I'd sugest looking at your way to measure angles, etc.

cbk1994 12-16-2008 11:14 PM

For angle, try
PHP Code:

angle = (pi 2) / * (dir 1); 

getAngle(x, y) works with a coordinate plane. For example, getAngle(-2, 1):

http://img213.imageshack.us/img213/3049/planexy2.png

So, to get the right result with getAngle(), you'd have to do something like this:

PHP Code:

angle getAngle(vecx(dir), vecy(dir)); 

which is far too much work when you can use a simpler solution.


All times are GMT +2. The time now is 02:38 PM.

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