MausGames Posted June 16, 2009 Share Posted June 16, 2009 (edited) I'm starting this thread as a place to ask questions about cycle-conscious coding methodology. My first question: What is the best way to check for zero joystick activity? If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then... Won't even compile. I can do: If !joy0up && !joy0down then a = 1 if a=1 && !joy0left && !joy0right then a = 2 if a = 2 && !joy0fire then a = 3 if a = 3 then... But it seems like there would be a much simpler way. Also, if the above example were changed to: If !joy0up && !joy0down then a = 1 if !joy0left && !joy0right && a = 1 then a = 2 if !joy0fire && a = 2 then a = 3 if a = 3 then... would that make any difference in cycles used? Is there any simple way to figure out which is the best order to put multiple conditionals in an if/then statement, or does it not matter? Edited June 16, 2009 by MausGames Quote Link to comment Share on other sites More sharing options...
jrok Posted June 16, 2009 Share Posted June 16, 2009 (edited) What is the best way to check for zero joystick activity? If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then... Won't even compile. There might be some other problem Maus. I do the above all the time, particularly when I want to store a joystick direction so that I can commit an action after the joystick is released. I've never had a problem with it. EDT: I missed the "joy0fire" part. Edited June 17, 2009 by jrok Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted June 16, 2009 Share Posted June 16, 2009 Does this compile? If !(joy0fire || joy0up || joy0down || joy0left || joy0right) then... Quote Link to comment Share on other sites More sharing options...
+batari Posted June 16, 2009 Share Posted June 16, 2009 if SWCHA>=240 then ... Quote Link to comment Share on other sites More sharing options...
BigO Posted June 16, 2009 Share Posted June 16, 2009 (edited) ... Edited June 16, 2009 by BigO Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 17, 2009 Share Posted June 17, 2009 Agreed. Reading the bB page it looks as if that should be possible. Actually, I might have a use for it and I've never done it before. Thanks for bringing it to my attention guys. What is the best way to check for zero joystick activity? If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then... Won't even compile. There might be some other problem Maus. I do the above all the time, particularly when I want to store a joystick direction so that I can commit an action after the joystick is released. I've never had a problem with it. Quote Link to comment Share on other sites More sharing options...
Robert M Posted June 17, 2009 Share Posted June 17, 2009 if SWCHA>=240 then ... This is correct, but you did not include the check of the firebutton... if (SWCHA>=240) && (!joy0fire) then If the reason this works is not clear. It is because the bits that indicate a joystick direction are bits 4-7 in the memory location labeled SWCHA. So the bBasic code joy0right is the same as code SWCHA{7}=0, joy0left == SWCHA{6}=0, joy0down == SWCHA{5}=0, and joy0up == SWCHA{4}=0 The bits 4-7 if they were used to represent a number would equal 16+32+64+128 = 240. The bits are set to zero if the joystick is pushed in that direction. So if the joystick is cnentered all 4 bits are set equalling 240. If the joystick is pressed in any direction that bit will be zero and the value in SWCHA will be less than 240. The joystick firebutton is from a different register than SWCHA. Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position. Here is a snippet from my shooter game: if joy0fire then Fire temp1 = SWCHA / 16 rem RLDU = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR Stnd GoLD GoLU GoL Stnd GoD GoU Stnd Fire rem -- Player shoots if playerShotYpos then ShotBusy playerShotXpos = playerXpos + 2 playerShotYpos = 21 ShotBusy player0x = playerXpos REFP0 = playerStatus goto PlayerKneel Stnd rem -- Player stands still. player0x = playerXpos playerStatus{0} = 0 REFP0 = playerStatus goto PlayerStand GoD GoU rem -- Player kneels and shows shield if avalable. player0x = playerXpos REFP0 = playerStatus if shieldStrength then playerStatus{0} = 1 : goto PlayerKneel playerStatus{0} = 0 goto PlayerStand GoR GoRD GoRU rem -- Move player to Right. if playerXpos < 136 then playerXpos = playerXpos + 1 player0x = playerXpos playerStatus{3} = 0 playerStatus{0} = 0 REFP0 = playerStatus goto AnimatePlayer GoL GoLD GoLU rem -- Move player to Left. if playerXpos > 16 then playerXpos = playerXpos - 1 player0x = playerXpos playerStatus{3} = 1 playerStatus{0} = 0 REFP0 = playerStatus goto AnimatePlayer Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 19, 2009 Share Posted June 19, 2009 Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position. Pretty cool. So is that a lot faster? I also have a question about this: on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR Stnd GoLD GoLU GoL Stnd GoD GoU Stnd I'm guessing GoRD means "go right-down," GoL means "go left" and so on, but what does Stnd mean? Is it simply short for "stand"? Thanks. Quote Link to comment Share on other sites More sharing options...
potatohead Posted June 19, 2009 Share Posted June 19, 2009 Another cool thing about on-goto is that it is consistent. Every time through your loop, it checks once, jumps once. Where it makes sense to use, it's very predictable. Quote Link to comment Share on other sites More sharing options...
Robert M Posted June 21, 2009 Share Posted June 21, 2009 Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position. Pretty cool. So is that a lot faster? It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete. I also have a question about this: on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR Stnd GoLD GoLU GoL Stnd GoD GoU Stnd I'm guessing GoRD means "go right-down," GoL means "go left" and so on, but what does Stnd mean? Is it simply short for "stand"? You are correct. Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands. Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 21, 2009 Share Posted June 21, 2009 It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete. . . . Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands. Thanks. Is it OK with you if I add an adapted version of this info to the bB page? Quote Link to comment Share on other sites More sharing options...
Robert M Posted June 21, 2009 Share Posted June 21, 2009 It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete. . . . Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands. Thanks. Is it OK with you if I add an adapted version of this info to the bB page? Sure, information is free. I will be happy to review it for correctness when you are done. Cheers Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 21, 2009 Share Posted June 21, 2009 Sure, information is free. I will be happy to review it for correctness when you are done. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.