Page 1 of 1

Help calling variables

Posted: Thu Nov 17, 2016 4:18 pm
by aflin
I've been having trouble calling a variable for a computing project I'm doing at school. The variable in question is called "enemyattack" and should be used to determine whether or not the player's attack is successful or not. In every attempt I've made to fix this the player loses, even when they should win, and after some investigation it would seem that my code isn't reading from the "enemyattack" variable when making its comparisons, but nothing I do is fixing it. The problem occurs within the buttons "skeleton", "slime" and "goblin" not getting the correct data. If it worked then slime would beat goblin, goblin would beat skeleton and skeleton would beat slime. I've yet to implement a "draw" command. Sorry in advance for the lack of description lines.

Re: Help calling variables

Posted: Thu Nov 17, 2016 4:22 pm
by aflin
SLIME BUTTON

on mouseUp playerattack, enemyattack
put 2 into playerattack
hide image "skeleton1" show image "slime1" hide image "goblin1"
if enemyattack = 2 then
send success to button "Launch"
else
send failure to button "Launch"
end if
end mouseUp


GOBLIN BUTTON (doesn't do anything, was an attempt to fix)

on mouseUp playerattack, enemyattack
put 3 into playerattack
hide image "skeleton1" hide image "slime1" show image "goblin1"
if enemyattack = 3 then
send success to button "Launch"
else if enemyattack = 1 or enemyattack = 2 then
send failure to button "Launch"
end if
end mouseUp

"Launch" Button, where everything is stored

global screenstatus, score, playerattack, enemyattack, timer

on mouseUp
setup_variables
set_visible
hide button "Launch"
end mouseUp


on setup_variables screenstatus, score, playerattack, enemyattack, timer
put 0 into screenstatus
put 0 into score
put 0 into playerattack
put 0 into enemyattack
put 0 into timer
end setup_variables

on set_visible
if screenstatus = 0 then
show button "Begin Ascent" show button "Hi-Scores" show button "RESET" hide button "Home"
else
hide button "Begin Ascent" hide button "Hi-Scores" show button "Home"
end if
end set_visible

command EnemySelect @enemyattack
put random(3) into enemyattack
if enemyattack = 1 then
show image "slime2" hide image "skeleton2" hide image "goblin2"
else if enemyattack = 2 then
show image "goblin2" hide image "skeleton2" hide image "slime2"
else
show image "skeleton2" hide image "slime2" hide image "goblin2"
end if
end EnemySelect

command HomePage screenstatus
put 0 into screenstatus
if screenstatus = 0 then
show button "Begin Ascent" show button "Hi-Scores" hide button "Home" hide graphic "oval" hide graphic "Playerarea" hide field "combat_text" hide button "skeleton" hide button "slime" hide button "goblin" hide image "skeleton1" hide image "slime1" hide image "goblin1" hide image "skeleton2" hide image "slime2" hide image "goblin2" hide image "playercross" hide image "enemycross" hide image "gameover"
else
hide button "Begin Ascent" hide button "Hi-Scores" show button "Home" hide field "combat_text"
end if
end HomePage



command success
wait 0.5 second
put score + 1 into score
show image "enemycross"
wait 1 second
hide image "enemycross"
send EnemySelect to button "Launch"
end success

command failure
wait 0.5 seconds
show image "playercross"
wait 0.5 second
show image "gameover"
wait 2 seconds
send HomePage to button "Launch"
end failure

Re: Help calling variables

Posted: Thu Nov 17, 2016 5:06 pm
by Klaus
Hi alfin,

1. welcome to the forum! :D

2. You are treating e.g. powerattack and enemyattack as parameters to your MOUSEUP message, but you need
to declare them as global in every script you use them, like this:

Code: Select all

global powerattack, enemyattack

on mouseUp
  put 2 into playerattack

  ## THREE separate lines neccessary!
  hide image "skeleton1" 
  show image "slime1" 
  hide image "goblin1"

  if enemyattack = 2 then

   ## Always put QUOTES around the message you want to send:
    send "success" to button "Launch"
  else
   send "failure" to button "Launch"
  end if
end mouseUp
Same for your other scripts...


Best

Klaus

Re: Help calling variables

Posted: Fri Nov 18, 2016 4:13 pm
by ghettocottage
The problem occurs within the buttons "skeleton", "slime" and "goblin" not getting the correct data..
this is the most entertaining post I have read in a long time. You may have just given me a new naming convention for my applications.

Re: Help calling variables

Posted: Mon Nov 21, 2016 10:55 am
by aflin
Thanks Klaus, your insight has been invaluable and has likely saved me from tearing my hair out over the next week. I'll be implementing the changes as soon as possible and I'll let you know if it works (if I remember to post that is).

Also you're welcome ghettocottage, but I just felt like this way of naming my variables made the most sense, no hilarity intended.