×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

basic sprite AI in director problems

basic sprite AI in director problems

basic sprite AI in director problems

(OP)
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

RE: basic sprite AI in director problems

I suppose you'll need to introduce some randomness into your enemy character's movement. Perhaps something like change its course to random direction at random intervals?

Kenneth Kawamoto
www.materiaprima.co.uk

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close