CodeRecipe( );

one stop for all game coders..

Follow us on Twitter
Home
Some AI using timer object Print E-mail
User Rating: / 1
PoorBest 
Written by CR Team   
Thursday, 13 August 2009 13:35

One can create some basic AI game logic using the timer objects available in Adobe Director scripting using the timeOut() method.katomic

Say you have a bunch of enemies to be spawned and they have to shoot continuously after a specific interval,( say a enemy ship approaching a hero ship ), you can create a timer object that would call a method specified after the given interval in milliseconds.

The following code snippet will walk you through the process:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
global alien_spawn, alien_missile_spawn
--alien_spawn and alien_missile_spawn contains time in
-- Milli-seconds
--make them global so that you can assign values
--anywhere in the game, suppose u want to make the game tougher
--as the level goes up
on timer_control alien_spawn, alien_missile_spawn
--speed based on score or level can be increased
--time interval between enemy spawns
myTimer1 = timeOut().new("createAliens",alien_spawn,#_init_enemy)
--timer for the missile from enemy ship
myTimer2 = timeOut().new("alien_missiles",alien_missile_spawn,\
#missileFire)
end

In the above code snippet, #_init_enemy and #missileFire are user defined functions that will be called when the timer tick is completed. Say you have specified the alien_spawn to be 3000 ms, the _init_enemy() function will be called every 3 seconds.

Once the above code is setup one can define the function and go about the game logic:

1
2
3
4
5
on _init_enemy
--create the enemy model from its resource
--create rigid body out of the enemy model
put "enemy_created :)"
end

Suppose you want to remove the timer just call the forget() method with the timer name:

1
2
3
4
on forget_timer
timeout("createAliens").forget()
timeout("alien_missiles").forget()
end

Hope this was helpful. Share more ideas if any at our forums :)


 

CR Login

CodeRecipe Store

Twitter Feed

CR :) >>: RT @indiegamescom: Student game dev competition Dare to be Digital now accepting applicants http://t.co/Hf1phWJG