Spriteimage appear and disappear in random position

596 views
Skip to first unread message

Alan

unread,
Mar 1, 2018, 12:28:58 PM3/1/18
to MIT App Inventor Forum
How to make the Spriteimage appear and disappear in random position in the block. please advise. thanks

SteveJG

unread,
Mar 1, 2018, 1:03:56 PM3/1/18
to MIT App Inventor Forum
Perhaps you might read 


and be aware you can make an ImageSprite visible by setting ImageSprite.Visible to true and hide the sprite by setting it to false.

Alan

unread,
Mar 2, 2018, 3:03:43 AM3/2/18
to MIT App Inventor Forum
Thanks for your advise. But i wanted to know how to make the imagesprite appear in random postions not move to  x,y. Thanks.

SteveJG

unread,
Mar 2, 2018, 8:32:20 AM3/2/18
to MIT App Inventor Forum
Actually x,y is a random position, at least until the developer assigns a value to the x (horizontal value) and y (the vertical value).  The text in Chapter 17 shows how the Canvas coordinates are 'numbered'.

Do you know how to use the Math blocks Alan?                             The Math blocks are described here: http://appinventor.mit.edu/explore/ai2/support/blocks/math.html  and the one you need is 

random integer

Returns a random integer value between the given values, inclusive. The order of the arguments doesn't matter.



From reading Chapter 17, you certainly know that if the Canvas is 300 x 300 pixels the y axis coordinates are numbered left to right from 0 to 299 do you think?   What about the y coordinates?
The top of the Canvas screen y coordinate is 0 ( the topmost and leftmost pixel has coordinates of ( 0,0 ). So the possible locations range somewhere with an x value of 0 to 299 and a y value of 0 to 299.

If you know how to send the ImageSprite to  a specific coordinate, you can send it to a random coordinate.      If you connect   a random block to each of the x and y in the set ImageSprite1.MoveTo block  and fill in the range of values, where do you think the sprite will appear? 

Blocks like this:   x = random integer from 0 to 299   might work.   You also need a y value.



What blocks have you tried so far Alan?   This is your Project.  You have all the information you need to complete the App.

When you try it, depending on the dimensions of your ImageSprite you may have an issue.  Do you know what it is?   Read the ImageSprite documentation   http://ai2.appinventor.mit.edu/reference/components/animation.html#ImageSprite  .      Look at the fourth Method.  It says something about the ImageSprite and what the x and y refers to.  That means that the x, y is NOT the center of the sprite.  Somehow you have to compensate for that.   How wide is the sprite?  Will that affect how you select either the maximum/minimum random number range to determine?    Is this complicated?  Yes. So it is time to experiment.   Ignore this advice for now, get the sprite to randomly appear and watch your screen to see what happens eventually.

Regards,
Steve



Abraham Getzler

unread,
Mar 2, 2018, 9:35:32 AM3/2/18
to MIT App Inventor Forum
The Canvas has blocks for .Height and .Width that you can use for your
y and x limits, respectively.

Leave yourself room for the .Height and .Width of the Sprite, to,
so it doesn't get clipped at the edge of the Canvas.

ABG

Reply all
Reply to author
Forward
0 new messages