Image sprite name references

124 views
Skip to first unread message

David Lane

unread,
Feb 9, 2014, 2:07:11 PM2/9/14
to mitappinv...@googlegroups.com
Hi,

I'm writing to ask about methods of referring to image sprites, having just written an abacus app which had 100 sprites on the screen.  In order to change the colour of these (by swapping the picture attached to each one), I had to have 10 lists, with each list containing the names of 10 of the image sprites of the 100 beads.

As a Director programmer, I'm used to being able to refer to the name of a sprite directly, and I had taken the approach of creating the names of the sprites in a repeat loop-but found that this would not work.  I hope that the attached screenshot of my blocks will make it plain (this example has just 3 sprites).  On the left is a list-based procedure that works, on the right is the procedure that fails.

It would be handy to have some method (perhaps a new block) that returned a reference to a sprite upon being passed its name, or allowed the programmer to provide a component name in the form of a string.  Unless there is a way of doing this that I have missed?

Many thanks...








SpriteNameReferences.gif

Taifun

unread,
Feb 9, 2014, 2:31:53 PM2/9/14
to mitappinv...@googlegroups.com
the left example is the way to go currently...
you can simplify that procedure a little bit by using a for each in list block instead
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

for each in list

Runs the blocks in the do section for each item in the list. Use the given variable name, i to refer to the current list item.

Hal Abelson

unread,
Feb 9, 2014, 3:14:04 PM2/9/14
to David Lane
Hi David,

This is a very nice example.  It doesn't have to do with sprites or components, but is fundamentally about how App Inventor treats names.   In App Inventor, a name is an identifier that specifies a value.   For example, you might have [define mynumber5 1007] so that the identifier mynumber5 now refers to 1007.

But there is no way to take the string "mynumber" and the string "5" and join them together to get the string "mynumbert5" and use that string to get to 1007.

That's what you're trying to do on the right-hand set of blocks when you create a text string like "ImageSprite5" and attempt to get from that to the object whose name is ImageSprite5.

That's a perfectly reasonable thing to want to do.  We have not included that in App Inventor, although we could.    You could do it in Java with an extension that calls the compiler.  For Python or Javascript you could use eval.  So we'll think about it, but it can be a pretty confusing feature for our App Inventor target audience.

Thanks for suggesting this.

But as Taifun says, stick with the style on the left, where you refer to the sprites themselves rather than manipulating their names.





--
You received this message because you are subscribed to the Google Groups "MIT App Inventor Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mitappinventortest+unsub...@googlegroups.com.
To post to this group, send email to mitappinventortest@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mitappinventortest/09a2ec46-d720-401b-b92d-d6e06c34bc25%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

David Lane

unread,
Feb 10, 2014, 4:29:19 AM2/10/14
to mitappinv...@googlegroups.com

Hi,

many thanks for the replies.  If possible, it would be great for a future release to include a couple of new blocks that:
convert a number to a string
convert a string to a number
when passed a string (eg "ImageSprite_22"), returns a reference to that image sprite.

In creating my abacus app, I had to manually create 100 names (for a 10x10 array of the names), which was a bit clunky.

Thanks, though,  for a brilliant resource and introduction into Android programming!
Reply all
Reply to author
Forward
0 new messages