none
difficulties using array to load and show images RRS feed

  • Question

  • ' using this to load pictures works
    ' Load graphics
    path = Program.Directory
    background = ImageList.LoadImage( path + "/images/bg1.png")
    playerShip = ImageList.LoadImage( path + "/images/PShip.png"
    ' then using this to show the bg image also works
    GraphicsWindow.DrawImage( background, 0, 0 )

    Now i'm trying to upgrade this so that each stage of the level has a different background image. I'm trying to do this using an array but can't get it work.


    Notice i'm how trying to use and array to load and show the pictures
    ' Load graphics path = Program.Directory stages = 8 'stages per level, must also be this many bg pictures For stage = 1 to stages background[stage] = ImageList.LoadImage( path + "/images/bg" + stage + ".png") EndFor playerShip = ImageList.LoadImage( path + "/images/PShip.png") 'then i try to show the first bg image like this
    GraphicsWindow.DrawImage( background[stage], 0, 0 )

    ' next is to display the next image from array for the next stage

    stage = stage + 1
    if stage > stages then
    stage = 1
    endif
    ' change bg
    GraphicsWindow.DrawImage( background[stage], 0, 0 )

    I don't get any errors when i execute the program. It program starts and runs fine but none of the backgrounds appear.

    In an attempt to debug i placed the following two lines after the array that loads the graphics.

    TextWindow.WriteLine(background)
    TextWindow.WriteLine(playerShip)

    The textwindow looks like this:

    1=ImageList1;2=ImageList2;3=ImageList3;4=ImageList4;5=ImageList5;6=ImageList6;7=ImageList7;8=ImageList8;

    ImageList9

     

    Any advice would be appreciated.

     

     

    Friday, July 16, 2010 1:48 AM

Answers

  • You have loaded your images, but your problem is in GraphicsWindow.DrawImage( background[stage],0,0)

    Try use:

    stage = 1

    GraphicsWindow.DrawImage( background[stage],0,0)

    Because after that For your stage is 9 not 8 and you don't have image in backgound[9].

    I think your code work well if you just reset stage after that For


    Sorry My Bad English
    • Proposed as answer by Oskariok Friday, July 16, 2010 7:07 AM
    • Marked as answer by litdevModerator Wednesday, July 21, 2010 6:25 PM
    Friday, July 16, 2010 7:07 AM

All replies

  • You have loaded your images, but your problem is in GraphicsWindow.DrawImage( background[stage],0,0)

    Try use:

    stage = 1

    GraphicsWindow.DrawImage( background[stage],0,0)

    Because after that For your stage is 9 not 8 and you don't have image in backgound[9].

    I think your code work well if you just reset stage after that For


    Sorry My Bad English
    • Proposed as answer by Oskariok Friday, July 16, 2010 7:07 AM
    • Marked as answer by litdevModerator Wednesday, July 21, 2010 6:25 PM
    Friday, July 16, 2010 7:07 AM
  • I'm pretty sure i had already tried resetting stage = 1 but i tried it again and it still doesn't work.

    Any other suggestions?

    Friday, July 16, 2010 2:11 PM
  • Oskariok is correct, when you use stage it is the value at the end of the loading loop+1 (i.e. 8+1 = 9) and this image doesn't exist; another way to display the images sequentially would be the following.  This definitely works for me so if you have problems, then I would check the path and file names carefully.

    ' Load graphics
    path = Program.Directory

    stages = 8 'stages per level, must also be this many bg pictures
    For stage = 1 to stages
      background[stage] = ImageList.LoadImage(path+"/images/bg"+stage+".png")
    EndFor
    playerShip = ImageList.LoadImage(path+"/images/PShip.png")

    'then i try to show the first bg image like this

    While ("True")
      For stage = 1 to stages
        GraphicsWindow.DrawImage(background[stage], 0, 0 )
        Program.Delay(100)
      EndFor
    EndWhile

    Friday, July 16, 2010 5:43 PM
    Moderator
  • I disagree, stage is reset by the If statement.

    I ran the code and it displays bg1.

    I added a loop and delay and the code ran fine, displaying all 8 images. 

    Friday, July 16, 2010 8:09 PM
  • Rushworks, you are right, I was looking at the first DrawImage after setting the images - however you do need to add some kind of loop like the While I used or some other - and the first DrawImage would obviously fail with stage set to 9.  The bottom line is it will work with a loop of some sort that uses stage = 1 To 8, assuming the images are found correctly.
    Friday, July 16, 2010 8:49 PM
    Moderator
  • I still haven't gotten this coding method to work.

    litdev's code above works fine. But that's not exactly what i'm doing. I'm not trying to do a slideshow.

    After loading all the images the program goes to screen explaining the game. Once they start the game a call a subroutine that sets starting variables for the game (including stage = 0) then another subroutine called levelUp that increases game difficulty and includes

    stage = stage + 1
    GraphicsWindow.DrawImage( background[stage], 0, 0 )

    Each time they clear a level i call the levelUp subroutine again to change the background to the next image. But this coding method is not working for me. I've checked the image pathnames more times then i can count.  So i'm not using a tight loop since so many things happen between each image change.

    Notice i keep saying this coding method isn't working. I found a way around it. Instead of using an array i bring in each image separately using:

    bg1 = ImageList.LoadImage( path + "/images/bg1.png")
    bg2 = ImageList.LoadImage( path + "/images/bg2.png")
    ...etc...

    In the levelUp subroutine i use a long if statement to check what level were going to and set the appropriate image. To me it makes for longer, bloated, ugly, inflexible code - but it works. I'd prefer to use the array, but it's as if Small Basic interprets background[stage] and a named array instead of interpretting stage as a variable and resolving it to it's value.

    Sunday, July 18, 2010 5:09 AM
  • What you are trying to do should work with arrays - is it possible to upload a full version of your prototype code using arrays that shows the problem.

    In terms of degugging yourself, the first thing I would do is add a TextWindow.WriteLine("load background "+stage+" "+background[stage]) just before it is set.


    Sunday, July 18, 2010 9:48 AM
    Moderator
  • The source will be available here for a day or so:

    www.trep4.com/_litdev/Chaseteroids_for-litdev.sb

    I added comments consisting of a long line of "---------------------------------" to help you see the relevant parts.

    Lot more features planned for this. But right now the biggest thing is to fix bugs with a complete rewrite of onkey events.

     

    Sunday, July 18, 2010 8:14 PM
  • You can use the SmallBasic Publish option to share code - a website like you used is also easy.

    Without knowing exactly what should happen with your code, I have a test directory (images) with some sample png's - you have already added the debugging TextWindow.WriteLine statements.

    With these two I get my images displayed in the GrahicsWindow whenever the TextWindow debug suggests shows they should (code inside the --------------------------.  How do I see the problem.

    Its hard to see the problem without the full code, but I also can't reproduce a problem I can see with your code, never sure I have all the images required and do the right game play to see the issue (by the way your code looks nicely structured).  So can you think about a simplified example program that shows the issue.

    I guess the idea is to break the program down to its simplest form that shows a problem.  Sorry not much use, but its a process of elimiation.

    Sunday, July 18, 2010 8:58 PM
    Moderator
  • Here are the images that go with the source code.

    www.trep4.com/_litdev/images.zip

    In the source if you comment out the array parts and uncomment the older lines you'll see the first bg get used.

    From this area uncomment the last line and comment everything else:

    ' --------------------------------------------------------------------
    ' load background images using array
    stages = 8 'levels per wave, must also be this many bg pictures
    For stage = 1 to stages
      background[stage] = ImageList.LoadImage(path+"/images/Chaseteroids_bg"+stage+".png")
    EndFor
    'background = ImageList.LoadImage( path + "/images/Chasesteroids_bg1.png")
    '-----------------------------------------------------------------------

    From this area uncomment the last line and comment out everything else:

    '------------------------------------------------------------------------
    ' set the first bg
      TextWindow.WriteLine("load background "+stage+" "+background[stage])
      GraphicsWindow.DrawImage(background[stage], 0, 0 )
    '------------------------------------------------------------------------
     
      '  GraphicsWindow.DrawImage( background, 0, 0 )

     

    Now when you play you'll see the first bg image on all the levels. The goal is to change the bg with each level.

    Sunday, July 18, 2010 11:43 PM
  • Thanks for your trouble litdev. I got it working using the array. If it wasn't for you i wouldn't have kept trying to figure out the problem and settled for ugly code instead.
    Monday, July 19, 2010 1:17 PM