Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic sprite AI in director problems

Status
Not open for further replies.

leearach2004

Technical User
Dec 8, 2005
86
GB
Hi there i am new to director programing but understand the basics of lingo. I am creating a simple childs game that has one character that needs to collect objects by moving around a 2d screen that had walls in certain places. This works fine as does the wall hit test.

But i also want another charcter that will move all around the screen that you have to avoid. I put together the following code from some examples for the enemy charcter, it works kinda by changing direction when a wall is hit but it this also means that it does not walk around the whole screen somtimes it does other times just goes in a circle.

Code:
global movemalc, malcdir

-- exit frame event handler
on exitFrame me
  -- if left key pressed
  if (movemalc = 1) then
    -- set sprite to left direction member
    sprite(26).member = "malcleft"
    -- set sprite's width and height
    sprite(26).width = 25
    sprite(26).height = 25
    -- set malc direction to l
    malcdir ="l"
    -- set move function
    movem(me,-5,0)
    
    --if right key pressed
  else if (movemalc = 2) then
    -- set sprite to right direction member
    sprite(26).member = "malcright"
    -- set sprite's width and height
    sprite(26).width = 25
    sprite(26).height = 25
    -- set malc direction to r
    malcdir ="r"
    -- set move function
    movem(me,5,0)
    
    -- if down key pressed
  else if (movemalc = 3) then
    -- set sprite to down direction member
    sprite(26).member = "malcdown"
    -- set sprite's width and height
    sprite(26).width = 25
    sprite(26).height = 25
    -- set malc direction to b
    malcdir ="b"
    -- set move function
    movem(me,0,5)
    
    -- if up key pressed
  else if (movemalc = 4) then
    -- set sprite to top direction member
    sprite(26).member = "malctop"
    -- set sprite's width and height
    sprite(26).width = 25
    sprite(26).height = 25
    -- set malc direction to t
    malcdir ="t"
    -- set move function
    movem(me,0,-5)
  end if
  
end

-- move function
on movem me, dx, dy
  
  -- get this sprite rect and add change to it
  oldRect = sprite(me.spriteNum).rect
  newRect = oldRect + rect(dx,dy,dx,dy)
  
  --set hitWall to false
  hitWall = FALSE
  
  -- loop through wall sprites 2 to 22 and detect collisions
  repeat with i = 2 to 21
    -- if sprite returns hitwall = true
    if sendSprite(i,#hitWall,newRect) then
      -- set hitwall to true
      hitWall = TRUE
      moved
      -- exit repet statment
      exit repeat
    end if
    -- end repeat statment
  end repeat
  
  -- if no collision, then move sprite
  if not hitWall then
    -- set sprite location to dx dy co-ordinates
    sprite(me.spriteNum).loc = sprite(me.spriteNum).loc + point(dx,dy)
  end if
end

-- move malc function
on moved
  
  -- check previous direction
  if(malcdir = "l") then
    movemalc =  4
  else if(malcdir = "r") then
    movemalc = 1
  else if(malcdir = "t") then
    movemalc = 2
  else if(malcdir = "b") then
    movemalc = 3
  end if
end

anyone know any simple ai tutrials or can sugest where i am going wrong would be great

thanks in advance to all

lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top