2017년 7월 18일 화요일

Ball, Canvas: Bounce a ball off a drawing on the Canvas


Kelly from the App Inventor in Education forum asked about bouncing a ball off of a line on the canvas. I thought I would include my solution here. The key is to detect the color under the ball and to have the ball bounce when it touches a specific color or any color that is not the background color. This particular solution looks for a red object and if it touches it, bounces. Both timers are needed for the ball to not pass through a thin line.

Blue ball, red line, white canvas:

Timers can be configured in the Screen Designer:



Timer settings were included here to show what worked for me, but they can be set up in the Screen Designer instead:



--
I have been created a maze game for a school assignment and it took me awhile to get the maze generation working but now a new problem has arisen. Even using this method my imagesprite still goes through the walls sometimes. Increasing the thickness of the walls from 2-4 also increases the chance of a successful rebound but tis is far to big for my test normal test device. I tried things like adjusting the speed of the player, disabling the player's input until the cooldown ends and tweaking the clocks timer intervals but atlas to no avail. I have considered checking all the corners and with the walls at a thickness of 4 it worked when moving left or right through walls (Up and down didn't work because the change in heading affected were the corners would be. ie the area it would be checking would now be below the sprite rather than at the corner.). I also tried using the negate block on the players speed property but this was disastrous, is getpixelcolour more effective than getpixelbackgroundcolour because at the moment that is the only difference I have (Besides setting player input to false - I do this by setting a variable to false then when the canvas is flung it needs that variable to be set to true for it to move the player).  I am considering doing more tweaking with the player's interval and the clock times. If you have nay help or suggestions it would be greatly appreciated as I am running out of time. Also if it helps the imagesprite is 20x12 and I haven't tested if it works with a ball yet. Thanks
EDIT: Okay so I now have it so the player can only move in 4 directions, currently I only have 2 of the directions set up to check all the corners of the image sprite but I can definitely see improvement however it is not fixed and rarely it will still go through walls (If the sprite stops close to the wall and then move again it will pass through the wall). Once again your help would be greatly appreciated.

EDIT 2: So its now set to recognise all directions but it doesn't pick up my up or down directions and left and right have the error mentioned above. Back to the drawing board.

--
Unfortunately, when using the Canvas, animation is not very responsive in some cases.
I would recommend setting your clock timer to an interval of 0 (zero) to make it as responsive as possible.
Other than that, I have struggled with the same issue as yourself -- sometimes the sprite does not bounce off the background color as expected.
It should not matter which block is used. Either GetPixelColor or GetBackgroundPixelColor should work the same.
GetPixelColor also detects the color of a sprite at the specified x,y location. That is how it is different.

--
I made this in App Inventor2, but cannot use a custom color.
Why is that?

--
@imans314: For general App Inventor questions please ask in the MIT App Inventor Forum.

--

댓글 없음:

댓글 쓰기