Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Getting runtime error when adding setFillcolor attribute to an array
Started by IndieEnthusiast Feb 11 2019 12:02 PM

- - - - -
17 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

ERROR: Runtime error
                    levelselection.lua:24: attempt to index field '?' (a nil value)
                    stack traceback:
                    levelselection.lua:24: in function '?'
                    ?: in function 'dispatchEvent'
                    ?: in function 'gotoScene'
                    mainmenu.lua:114: in function '?'
                    ?: in function <?:190>
 
Getting this error when I try to add display attribute to an array like setcolor, code :
 

myRoundedRect = display.newRoundedRect( 1, 1, 1, 1, 1 )

  for i = 1, 4, 1 do
    for j = 1, 5, 1 do

      myRoundedRect[i] = display.newRoundedRect( 225*i, 100*j, 80, 50, 12 )
      myRoundedRect[i]:setFillColor(1,2,1)
    end
  end

Basically this is helping me draw a level selection menu, but when I try to add display image attribute myRoundedRect[i]:setFillColor(1,2,1), the error above is  being thrown.

 

With a simple for loop was able to create this, I am focusing on minimizing code and do not want to repeat writing code for each display object, any idea?

 



[TOPIC: post.html]
#2

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

I would like to then later on depending on which myroundrect[i][j] is clicked, will transition to the correct level. I hope you get the picture?



[TOPIC: post.html]
#3

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

line 24 in your code refers to a nil value but you are not showing your code with line numbers...


  • IndieEnthusiast likes this

[TOPIC: post.html]
#4

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

Here is the screenshot

Attached Thumbnails

  • code.jpg


[TOPIC: post.html]
#5

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

When I use myRoundRect[i].setFilColor , then I get this error



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,686 posts
  • Corona SDK

Many things going on here. You should set up a proper table called myRoundedRect, instead you seem to be just creating a 1x1 rect and then adding all your rects to that. Yes, technically it's a table, but this isn't the right way to do it, and It's possible your bug is related to this. 

 

Put "local myRoundedRect = {}" at the top of your lua file. 

 

Secondly, every time you loop through j, you are overwriting the previous myRoundedRect[I] with a new one, and losing forever the reference to the previous one.

 

A colour of (1,2,1) is the same as (1,1,1). The maximum value is 1.

 

Lastly I don't think roundedRect is really suitable for actual use, it creates a very pixelated image. I would import my own rounded rectangle and use display.newImageRect.



[TOPIC: post.html]
#7

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

first you create myRoundedRect as a display object

then you try to loop through myRoundedRect as a table

that wont work

remove line 18 (or rename it something else)

under it type "myRoundedRect={}" to set it as a table

then the loop should work



[TOPIC: post.html]
#8

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

what nick said  ;)



[TOPIC: post.html]
#9

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

@indieEnthusiast

 

I notice that you are often enough posting problems and issues that you would not come across if you would have basic knowledge of lua and corona. Please note, I am not saying you dont know how to code in general, but lua and corona seems to be new for you.

 

It is clear that you have a desire to quickly make one or more apps, but if I may come with a piece of advice, the time you spend struggling with these issues could be better spent studying the corona documentation. it is very well written and if you give it one week, you will find that you quickly get the basics down right, and you're progress will speed up drastically. Maybe not as exciting but definitely worth it in the long run.


  • IndieEnthusiast likes this

[TOPIC: post.html]
#10

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

Hi anaqim,

 

You are absolutely right. The issue is I often do not get a lot of time to code. I work fulltime and by the time I am at home I mainly get 1 or 2 hours at night to code. I know if I spend more time I will get better at it. I guess I should be more patient. Thanks for your advice. When I get stuck, I think of so many experts in this forum who have helped me out previously. However there is no shortcut to learning. I will google and try to figure out myself first before asking here.


  • anaqim likes this

[TOPIC: post.html]
#11

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

Hi indieEnthusiast,

 

Thanks for not getting offended by my post.

I know posts like that can so easily get taken the wrong way.

 

There simply never is enough time, period  ^_^

Started myself some 2 years ago now and went through the same thing.

Corona docs was (and still is) my go to bible whenever I have a question or need to look something up.

 

I would advice against using google as the primary source for info as much is outdated. Corona Docs is straight to the point, always updated with examples and instantly available. What more do you need :) 

 

Have a great day!



[TOPIC: post.html]
#12

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 613 posts
  • Corona SDK

my advice is stop wasting 1 or 2 hours coding. spend them on reading corona api, corona book, or lua book or even online video tutorials.

after 2/4 weeks try to code without help.

 

Why people invert the process and start programming without having a clue with the basic stuff, beats me.

 

The time you spend to correct a lvl 0 error like this, you would have learned 100x more stuff reading a proper book.

 

My 2 cents.



[TOPIC: post.html]
#13

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

yeah but then there is this burning flame of creativity and enthusiasm that need feeding, and while logic dictate its better to study first, study doesnt feed that flame  :P


  • IndieEnthusiast likes this

[TOPIC: post.html]
#14

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

:unsure:  :unsure: Also the fact that back of the mind I know the big Corona Bosses resides in this forum...which can make one a little bit lazy 



[TOPIC: post.html]
#15

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

That is actually not quite true.

Almost all the people who answer here are users of various skill levels, and not employees.

This is likely why posts that dont make any sense or clearly are missing a lot of info, just go unanswered.


  • IndieEnthusiast likes this

[TOPIC: post.html]
#16

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

I think I will try to battle it out first myself the lua + corona codes. To be frank I am not very good with looping mechanism of while/for, it was the first time I used for loop, so the fact is I should have gone through more example codes and be more patient. Asking will not always improve learning. Constructive criticism is always good.



[TOPIC: post.html]
#17

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 213 posts
  • Corona SDK

Everything is now working as expected, last night took a little bit of time understanding lua arrays and the examples given. So now the level selection template is almost complete. I just need to give images to the placeholders and implement composer.gotoScene.



[TOPIC: post.html]
#18

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

:D




[topic_controls]
[/topic_controls]