php - Stopping while waiting for user input -
i'm making game. in game, player , enemies stored inside objects. each object has properties such as:
$player->health $player->attack (this value, in attack power)
then there's php function handles fight:
function fight($player, $enemy) {...}
it uses functions attack($player, $enemy)
, castspell($player,$enemy)
.
now, here problem: game runs on round system, player , enemy each has turn. want every time it's player's turn, script wait until user clicks, , function called according button pushed.
i thinking of using javascript settimeout , stopping when user clicks, objects , functions use in php.
how can otherwise?
you cannot wait user input in middle of php script: php script runs, generates output (usually html page), , exits. output sent user's browser, can read , interact it.
the interaction describe require use of ajax, initial page rendered , sent browser, , onclick
handlers or similar in js cause calls php script works out should happen based on event clicked. need persist objects, e.g. using $_session
, between various php calls.
all ajax means php script returns small piece of data, rather whole page. clicking "cast healing spell" might request "/ajax_fight_action.php?player=1&action=cast_spell&spell_name=healing" return (echo
) xml or json string lets js know new health of player.
Comments
Post a Comment