If block not detecting the value of a variable, but another if loop is, and the program will not detect the right information

That is the image of my arrow code. It detects when the arrow hits a wall, and then checks the state of the sword that is supposed to be blocking the arrow. The first detection is whether the sword is resting or attacking, and that part works fine. But the next part, where it is supposed to detect whether the sword is up, does not work. Even if my sword is in the up position, when that arrow hits the wall, it does damage anyway.

The weird part is that
a. The sword moves up and down like it is supposed to, so obviously the swordState is working.
b. All other positions work. Down and forward block like they are supposed to. I can find no reason that those if loops do not work.
c. I have 6 arrows, and the up is broken for all 6 of them.

Could someone please help or at least explain what the problem is?

You are probably following advice here

Something is probably not correct in your code here

This code only executes when the Arrow Y coordinate is exactly 40 . Perhaps try >= 40 ?

Debugging code that depends on Procedures that are unexplained or without checking on a device are extremely difficult to detect and fix. You haven’t provided all your Blocks or an aia so I offer you http://ai2.appinventor.mit.edu/reference/other/testing.html that jprovides advice that should help you debug this.

I would start exploring by Disabling all the blocks below the up state scenario. Then make sure your code for up works. Then get the other parts of your interesting game to work.


The arrow can only be 40, 117, or 183 (up, middle, or down positions). All other code, like ALL of it, works. The only thing that doesn’t is that pesky up block in all 6 arrows. Since I copied and pasted, the problem is probably the same in all of them.

Regarding your solution, I tried disabling the other blocks, which just breaks the game even more, since now the arrows are completely unblockable. Let me send you an image of all of my code…

This here is the compacted version. Below is everything important to this issue.

Above is the thing that shoots the arrows.
Called whenever this happens.
blocks (2)
And the sword updates when this stuff happens

It looks like that for each of the directional blocks.
This here procedure takes that and moves the swords around and such.

And finally here is the project aia.
FINAL_PROJECT.aia (227.4 KB)
If you could take a look at that, since you obviously have more experience then I do.
Personally I learned programming through some OOP Python and basic js.

I tried to self debug, but I couldn’t find any difference between that up state thing and the others.
Help would be much appreciated! :+1:

I tried to run your app, but found it unable to fit into the emulator.

Your three directional buttons are pushed far offscreen.

Also, your screen handling operations stack up Screen1 instead of allowing
your start screen to close.

You need to fix these before the rest.

StartScreen Play Click

Ok. I don’t have a fix. I can tell you these are current issues:

  1. How it works.
  • the sword position buttons do not work at all with the code you provided.
  • you use a different ImageSprite for each sword position. You shouldn’t do that I expect. Instead use a single ImageSprite for swordPosition and use the upButton, ForwardButton and DownButton to change the swordPosition image. That could work to block arrows but it won’t. here is why:

upsword Although an image has a transparent background, the ‘collision’ takes place at the image outline, not at the visible part of the image.

  • You are attempting to ’ block arrows’ using your swordUpdate Clock with a 75 ms interval… will never work with AI2’s graphics. A single blink is … The duration of an eye blink is on average 100–150 milliseconds

  • Your images for the swords are 277x185 pixels but you display a sword up at 100x120 pixels. this is not a killer issue but when you depend on the cpu to shrink the image at fast speeds, you are stressing the Android’s resources.

  • I would provide more information but I cannot get

  • Your arrows work beautifully!

  • I got the sword movement buttons to ‘work’ (kind of) by adding the circled block

No more time, sorry… perhaps something mentioned here will help you get this working. It is an interesting idea. Experiment and make it work for you!

Hi Abraham, nice catch. what it looks like on a tablet:

works for me. With a few ‘fixes’.

Your collision event decision tree looks off to me.
You do collision logic only when you hit the wall. (see your outside IF/then.)

I can’t see the sprites for the sword, but I would imagine
you want to have those intercept the arrow(s) before the wall?

(Edit - so the wall is invisible, between the knight and the incoming arrows. I thought maybe it was behind the knight, so I thought wrong. Sorry!)

This kind of app benefits from use of generic (Any Sprite) blocks to eliminate
lots of duplication, so you only have to fix things in one place vs 6 places.

If you keep a global list of your Arrow Component blocks,
you can reuse code by arrow number procedure parameters.

Likewise for your three sword sprites, you can keep them in a list to test if the other
sprite in an arrow collision is a sword (in the list of sword components) versus the knight (ouch).

Here are some generic app samples …
https://docs.google.com/document/d/1Ctf-tI-oRaoziGqb-V-h9ATAtVhCPvHY65EoVW-FpZA/edit#heading=h.qjerpbof05eg (buttons for dates in a calendar)
https://github.com/AGetzler/eight_queens_generic (The eight Queens problem in 64 buttons)
https://groups.google.com/d/msg/mitappinventortest/ck_CIVWObhw/vUr6nBcDAwAJ (pattern lock, not entirely generic yet)

If this is a final exam project, ask yourself how it will be graded.

Will the teacher have access to a big tablet to test it, with all the pixels needed to see the whole scenario?

If not, you might have to resize everything by proportion of Screen and Canvas Height and Width.

It is not a final exam project, sorry for the misleading name.

It is called that because it is an amalgamation of previous projects where I tested separate components of that.

Thanks for the helpful tips, I’ll try working on it tomorrow with what you guys have said.

Steve, the arrows trigger collisions when they hit this sprite called sword wall, which is in front of the swords. Maybe the sword points out in front of it. Actually I’ll look into that… but I don’t have time today

More hints that might help.

Oh, there it is (sword wall)

Thanks for the explanation. 'Maybe the sword points out in front of it.' ... it sure looks like that is true. For testing, you might want to set a picture to it (to make it 'visible' while testing). This might work bluesquare



Image shows arrow outline of 361x127 pixels in red that you display at 100x50. Crop your arrows to the green outline in your Media?

Okay I’ll crop the arrows right now…

Steve, what do you mean the sword buttons don’t work at all?
I tried them using my Acer tablet and the sword moves fine…

There is something I don’t understand. The lower sword, as you can tell, is much bigger. Yet it shows as the same size when I test the app?

[Edit] - I just cropped the arrow
[Edit 2] The reason the arrow is facing the opposite way is because of ai2’s heading thing. To make things go left, apparently the heading has to be 180, so my image is basically flipped. Is there a fix for this?
[Edit 3, sorry] Oh, and regarding tablet sizes, it looks messed up in the editor, but it works on phones and tablets for some strange reason. The tablet I use… I don’t know exactly what model, but it is similar in size to iPad mini 4 I guess, and it fits very snugly.
[Edit 4, really sorry] I have a question regarding the sword sticking out. When the arrow collides with that, will it do anything or just keep moving, since I have no code telling it what to do when that happens?

Here are a couple of things to make life easier for you …

If you are deciding hit/miss at the sword wall, you would want to turn Active false for your sword sprites, so they don’t grab the collision event.

The hit frame of a Sprite is determined by its declared height and Width attributes, not by its image.

  • what do you mean the sword buttons don’t work at all? Just what I said, when running the app using the companion on my Android 8.1 tablet, the buttons did not ‘move’ the sword. I added the call updateSword block in the event handlers following the swordState as I posted earlier and the sword display seem to change appropriately. Why they do not work without the additional block on my table but work on your Acer, I do not know.

  • The lower sword, as you can tell, is much bigger. Yet it shows as the same size when I test the app. Probably because you force the image to the size in your other blocks . the image I showed is how it displays in the Designer.

  • Arrows
    cropped :grinning: Actually not; this is your cropped arrow Cropped arrow
    The image is huge 687x135 pixels; not what you want. …and your image should be within the red boundaries; as it is the entire image dimensions are the arrow plus the purple. Try again?

    sticking out … well if the outline of the sword extends beyond the barrier (to the right) the arrow
    will collide with that sword. That is what happens when you use the CollisionWith blocks …it
    executes whenever it encounters any sprite.
    facing opposite direction you can handle that by using Paint or other software to make an
    additional arrow png (arrowPointedLeft.png) or using the arrow sprite heading and set it to 180


similarly, you could use only one sword sprite. How, use the single image and use the heading property to position the sword instead of multiple images.

You might reread Creating Animated Apps and also consider ABG’s hints

Have fun exploring.

I think this is what you said.... Though with your explanation now I understand what you meant. Sorry for the misunderstanding

oh and sorry for the weird crop thing. That was the wrong image