As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

Adobe Flex question.

SpeedySwafSpeedySwaf Registered User regular
edited April 2009 in Help / Advice Forum
We're doing a matching game for one of my classes, and one problem we're having is creating an image array to randomize the images. We're using Adobe Flex to do this. Anyway, we create an image array like this:
public function loadPics():void {
               for (var i = 0; i < 10; i++) {
                    picArray[i] = new String("domino" + (i+1) + ".PNG");
               }
            }

But then when we do something like this:
<mx:Grid x="240" y="49" width="139.4697" height="556.0606" creationComplete="loadPics()">
<mx:GridRow width="100%" height="100%">
            <mx:GridItem width="100%" height="100%">
                <mx:Image width="49.015152" height="100.98485" id="domino1" [B]source="picArray[0]"[/B] click="picClicked(0)"/>
            </mx:GridItem>
            <mx:GridItem width="100%" height="100%">
                <mx:Image width="50" height="96.969696" id="picArray[1]" [B]source="domino2.PNG"[/B] click="picClicked(1)"/>
            </mx:GridItem>
        </mx:GridRow>

the problem is that the "source" needs a string (it needs to be in quotations), and what this means is that when we pass it an array item, it thinks "picArray[0]" is the name of the image. What we need it to do is look at the string value in the picArray, and use that instead. Is their a way to do this?

SpeedySwaf on

Posts

  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited April 2009
    Try putting { } around picArray[0]

    source="{picArray[0]}"
    

    {} will call for an object value, whereas without {} that means you are either referencing a string or a function name if you include () at the end.



    Also consider embedding your domino pictures in the SWF so they load up on application init, instead of when they are "called" by the grid. That will make for a more responsive app, especially if it's being hosted online. Your teacher will give you bonus points! ;)

    Jasconius on
  • SpeedySwafSpeedySwaf Registered User regular
    edited April 2009
    Hmm, I got it changed to be bindable and everything, but now the image won't show up.

    I declare the pic array like so:
    [Bindable]
    public var picArray:Array = new Array(10);
    

    Use this to give the array a list of image names to use.
    public function loadPics():void {
        for (var i:int = 0; i < 10; i++) {
             picArray[i] = new String("domino" + (i+1) + ".PNG");
        }
    }
    

    Then use it like this to call the image.
    <mx:GridItem width="100%" height="100%">
                    <mx:Image width="49.015152" height="100.98485" id="domino1" source="{picArray.getItemAt(0)}" click="picClicked(0)"/>
                </mx:GridItem>
    

    No errors, but the image isn't showing up. Am I missing something?

    SpeedySwaf on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited April 2009
    Oh you would only do it bindable if you could specify it as a datasource for the control. Sorry, I deleted that part from my post after I read about the control. Doesn't look like you can bind to a Grid. Although that shouldn't stop it from working... but if you aren't binding it you may as well not add that.

    Also, don't use getItemAt().. kinda pointless

    picArray[0] should work fine and is faster.


    If you do that, and it still doesn't show up, make sure your images are in the right place.. for Flex Builder, root directory is the same as your MXML file. Also try setting AutoLoad="true" on the image control tag.



    hint
    picArray[i] = new String("domino" + (i+1) + ".PNG");
    

    is a bit overkill. Actionscript knows when you are doing a string, the same thing can be written
    picArray[i] = "domino" + (i+1) + ".PNG";
    

    Jasconius on
  • SpeedySwafSpeedySwaf Registered User regular
    edited April 2009
    Still nothing :? Even with the images in the root folder and everything.

    Thank you for the help, I'm probably on the right track but something small is getting in the way. I'll continue looking at it and ask around tomorrow and see what I can find.

    SpeedySwaf on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited April 2009
    Verify that creationComplete is actually firing when you want it to. I usually use the init event instead since I know that will fire before my control contents will be rendered.


    It's possible that your image controls are being printed out, and they are getting empty strings as file paths because creationComplete is firing after they are created. Strings in AS3 are passed by value, not by reference, so if they aren't populated yet, you would get nothing.


    Unless there's a good reason, you should be just populating that array on application init, not on control init.

    Jasconius on
Sign In or Register to comment.