如何处理此索引超出范围错误 (LINGO)

Posted

技术标签:

【中文标题】如何处理此索引超出范围错误 (LINGO)【英文标题】:what to do about this Index Out of Range Error (LINGO) 【发布时间】:2015-05-24 18:39:16 【问题描述】:

您好,我收到:脚本错误:以下索引超出范围

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

这是针对我有 10 名演员的 Cast:Cards 的。我在记忆游戏中有一个由 20 张卡片组成的矩阵,演员必须是矩阵数的一半。但是我无法克服这个无论我怎么努力都不会消失的错误。如果您能弄清楚我收到此脚本错误的原因,请提前感谢您。干杯。

放置在框架上的整个脚本如下:

     -- 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 over6
    -- 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 sprites8
    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 back7
    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

【问题讨论】:

【参考方案1】:

好吧,我只是让正在阅读的人知道,经过深思熟虑并一遍又一遍地查看代码,我终于解决了自己的问题。

这就是我所做的:

这里我注意到有两次添加列表的代码

-- 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

上面有

添加 pBoard,list[r]

在代码中一次,所以也许我需要添加另一个

因此我最终得到:

-- 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

这解决了我的错误,现在游戏正在运行。真是一种解脱!

【讨论】:

以上是关于如何处理此索引超出范围错误 (LINGO)的主要内容,如果未能解决你的问题,请参考以下文章

Vigenere密码,如何处理超出字符值范围的序数值

Swift 错误:索引超出范围

准备转场时致命错误索引超出范围

Python - 索引错误 - 列表索引超出范围

UICollectionView 给出“索引超出范围”错误

我收到错误:致命错误:索引超出范围