0

Hello I am getting: Script Error: Index Out of Range for the following

       sprite(spriteNumber).member = member(pBoard[cardNumber],"Cards")

This is for a Cast:Cards where I have 10 cast members. I have a matrix of 20 cards in a memory game and the cast members have to half the matrix number. But I can't get over this error that won't go away no matter how hard I try. Thanking you in advance if you can figure out why I'm getting this script error. Cheers.

The whole script which is placed on the frame reads:

     -- Settable Properties
     property pSpriteOffset, pDisplayDelay, pGameOverFrame
     property pClickSound, pMatchSound, pNoMatchSound
     property pNumberOfCards -- number of cards in the game
     property pBoard -- holds a list of what cards are where
     property pCard1 -- the first card in a pair clicked on
     property pCard2 -- the second card in a pair clicked on
     property pCardTimer -- the time that the second card was clicked


     on getPropertyDescriptionList me
       list = [:]

  -- pSpriteOffset is used to determine how far from
  -- the top of the Score the first card is
  -- so, if the first card starts in channel 11,
  -- the offset would be 10 (10 away from channel 1).
  addProp list, #pSpriteOffset,\
    [#comment: "Card Sprite Offset",\
     #format: #integer,\
     #default: 0]

  -- pDisplayDelay is how many ticks the pair of
  -- cards will be kept on the screen for the player
  -- to see before they are removed or turned back over
  addProp list, #pDisplayDelay, \
    [#comment: "Display Delay",\
     #format: #integer,\
     #default: 60,\
     #range: [#min: 0, #max: 240]]

  -- when a sprite is clicked, what sound is played?
  addProp list, #pClickSound,\
    [#comment: "Click Sound",\
     #format: #sound,\
     #default: "click sound"]

  -- when a sprite is matched, what sound is played?
  addProp list, #pMatchSound,\
    [#comment: "Match Sound",\
     #format: #sound,\
     #default: "match sound"]

  -- when a sprite is not matched, what sound is played?
  addProp list, #pNoMatchSound,\
    [#comment: "No Match Sound",\
     #format: #string,\
     #default: ""]


  -- when all sprites are matched, which frame should the movie go to?
  addProp list, #pGameOverFrame,\
    [#comment: "Game Over Frame",\
     #format: #marker,\
     #default: #end]
  return list
     end


     -- shuffles the cards to build the pBoard list
     -- initializes the game properties
     on beginSprite me


  -- refers to the "Cards" cast library to see how many cards there are
  pNumberOfCards = the number of members of castLib "Cards"


  -- build a list with each card in the list twice
  list = []
  repeat with i = 1 to pNumberOfCards
    add list, i
    add list, i
  end repeat


  -- fill the pBoard list up randomly with items from
  -- the previously created list
  pBoard = []
  repeat while list.count > 0
    r = random(list.count)
    add pBoard, list[r]
    deleteAt list, r
  end repeat


  -- initialize the game properties
  pCard1 = 0
  pCard2 = 0
     end


     -- called by the sprites when the user clicks
     -- the spriteNumber parameter is the sprite number clicked
     on turnCard me, spriteNumber
  -- play sound, if there is one
  if pClickSound <> "" then puppetSound 1, pClickSound


  -- determine the card number
  cardNumber = spriteNumber - pSpriteOffset


  if pCard1 = 0 then -- first card clicked
    -- record this card
    pCard1 = cardNumber
    -- turn it over
    sprite(spriteNumber).member = member(pBoard[cardNumber],"Cards")


  else if pCard2 = 0 then -- second card clicked
    -- ignore if it is the same card
    if cardNumber = pCard1 then exit
    -- record this card
    pCard2 = cardNumber
    -- turn it over
    sprite(spriteNumber).member = member(pBoard[cardNumber],"Cards")
    -- set the timer
    pCardTimer = the ticks


  else -- two cards are already turned over{6}
    -- this happens if the user clicks very quickly
    -- force a look at the two cards
    returnCards(me)
    -- make sure the card was not clicked on twice
    if sprite(spriteNumber).memberNum = 0 then exit
    -- record new card as the first card
    pCard1 = cardNumber
    -- turn it over
    sprite(spriteNumber).member = member(pBoard[cardNumber],"Cards")
  end if
     end


     -- looks at the two cards turned over and compares them
     on returnCards me
  if pBoard[pCard1] = pBoard[pCard2] then -- they are a match
    -- play sound, if there is one
    if pMatchSound <> "" then puppetSound 2, pMatchSound
    -- remove both sprites{8}
    sprite(pCard1+pSpriteOffset).memberNum = 0
    sprite(pCard2+pSpriteOffset).memberNum = 0
    -- check for game over
    if checkAllMatched(me) then
      go to frame pGameOverFrame
    end if
  else -- no match
    -- play sound, if there is one
    if pNoMatchSound <> "" then puppetSound 2, pNoMatchSound
    -- turn both cards back{7}
    sprite(pCard1+pSpriteOffset).member = member("Card Back")
    sprite(pCard2+pSpriteOffset).member = member("Card Back")
  end if


  -- reset the game properties
  pCard1 = 0
  pCard2 = 0
     end


     on exitFrame me
  if pCard1 <> 0 and pCard2 <> 0 then -- two cards are turned
    if the ticks > pCardTimer + pDisplayDelay then -- time has expired
      -- check the cards to see if there is a match
      returnCards(me)
    end if
  end if



  -- loop on the frame
     _movie.go(_movie.frame)
       end


     -- check to see if the game is over
     on checkAllMatched me
  -- loop through all cards
  repeat with i = 1 to pNumberOfCards
    -- determine the card's sprite
    spriteNumber = i + pSpriteOffset
    -- if it is still a card, then the game is not over
    if sprite(i).memberNum <> 0 then return FALSE
  end repeat


  -- all cards missing, so game over
  return TRUE
     end

     and this is the script that is placed on the sprite in the matrix:

     on mouseUp me
  -- simply tell the frame script that this sprite was clicked
  sendSprite(0,#turnCard,me.spriteNum)
     end
botero
  • 598
  • 2
  • 11
  • 23
londonbird
  • 45
  • 1
  • 2
  • 7

1 Answers1

0

Well I'm just letting anyone who is reading know that I finally solved my own problem after much thought I and looking at the code over and over.

This is what I did:

Here I noticed that there is code to add a list twice

-- build a list with each card in the list twice
  list = []
  repeat with i = 1 to pNumberOfCards
    add list, i
    add list, i
  end repeat

so I thought, what I'm getting is only one part of this list on my matrix and I finally got a bright idea that somewhere in this code there is another list to add. So I looked carefully and finally assumed that:

 -- fill the pBoard list up randomly with items from
  -- the previously created list
  pBoard = []
  repeat while list.count > 0
    r = random(list.count)
    add pBoard, list[r]
    deleteAt list, r
  end repeat

the above has

add pBoard,list[r]

in the code once so maybe I need to add another

Therefore I ended up with:

-- fill the pBoard list up randomly with items from
  -- the previously created list
  pBoard = []
  repeat while list.count > 0
    r = random(list.count)
    add pBoard, list[r]
     add pBoard, list[r]
    deleteAt list, r
  end repeat

which solved my error and now the game is working. What a relief!!

londonbird
  • 45
  • 1
  • 2
  • 7