sorry,I'm newbie,who can help me use for-loops change display.newcircle() to create 20 circle

display.setStatusBar( display.HiddenStatusBar )

display.setDefault( “background”, 80/255 )

local function newBall( params )

local xpos = display.contentWidth*0.5

local ypos = display.contentHeight*0.5

local circle = display.newCircle( xpos, ypos, params.radius );

circle:setFillColor( params.r, params.g, params.b, 255 );

circle.xdir = params.xdir

circle.ydir = params.ydir

circle.xspeed = params.xspeed

circle.yspeed = params.yspeed

circle.radius = params.radius

return circle

end

local params ={

{ radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 },

{ radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 },

{ radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 },

– newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 }

}

local collection = {}

– Iterate through params array and add new balls into an array

for _,item in ipairs( params ) do

local ball = newBall( item )

collection[#collection + 1] = ball

end

– Get current edges of visible screen (accounting for the areas cropped by “zoomEven” scaling mode in config.lua)

local screenTop = display.screenOriginY

local screenBottom = display.viewableContentHeight + display.screenOriginY

local screenLeft = display.screenOriginX

local screenRight = display.viewableContentWidth + display.screenOriginX

function collection:enterFrame( event )

for _,ball in ipairs( collection ) do

local dx = ( ball.xspeed * ball.xdir );

local dy = ( ball.yspeed * ball.ydir );

local xNew, yNew = ball.x + dx, ball.y + dy

local radius = ball.radius

if ( xNew > screenRight - radius or xNew < screenLeft + radius ) then

ball.xdir = -ball.xdir

end

if ( yNew > screenBottom - radius or yNew < screenTop + radius ) then

ball.ydir = -ball.ydir

end

ball:translate( dx, dy )

end

end

Runtime:addEventListener( “enterFrame”, collection );

    

@SkyKing0225,

Welcome to the community.  Please note, while I’m happy to help and will try in a moment, that was a terrible post.

A post should have these parts:

  • Clear and Concise Subject Oriented Title
    • sorry,I’m newbie,who can help me use for-loops change display.newcircle() to create 20 circle is not a title. :)  
  • Legible and well formatted body.
    1. Always format code posts with the <> button above the posting area.  Then, re-edit them to make them neat and easy to read.
    2. When posting about a bug or issue tell us:
    • What you did
    • What you saw (the result)
    • What you expected to see
    • Why you think the result is wrong.
    1. When posting a question (like this post), … post the question in a clear sentence or paragraph so we can easily read it.  Don’t got into more detail than needed to get the idea across.  Nobody likes to read long posts.

In this case you post could have been: 

(the title) Create Circles In Loop?

Hi.  I’d like to create 20 circles using a loop and i’m having trouble doing so.  Here is the code I have so far. Can someone help me figure out what I’m doing wrong?

local function newBall( params ) local xpos = display.contentWidth\*0.5 local ypos = display.contentHeight\*0.5 local circle = display.newCircle( xpos, ypos, params.radius ); circle:setFillColor( params.r, params.g, params.b, 255 ); circle.xdir = params.xdir circle.ydir = params.ydir circle.xspeed = params.xspeed circle.yspeed = params.yspeed circle.radius = params.radius return circle end local params = { { radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 }, { radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 }, { radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 }, -- newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 } } local collection = {} -- Iterate through params array and add new balls into an array for \_,item in ipairs( params ) do local ball = newBall( item ) collection[#collection + 1] = ball end -- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua) local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX function collection:enterFrame( event ) for \_,ball in ipairs( collection ) do local dx = ( ball.xspeed \* ball.xdir ); local dy = ( ball.yspeed \* ball.ydir ); local xNew, yNew = ball.x + dx, ball.y + dy local radius = ball.radius if ( xNew \> screenRight - radius or xNew \< screenLeft + radius ) then ball.xdir = -ball.xdir end if ( yNew \> screenBottom - radius or yNew \< screenTop + radius ) then ball.ydir = -ball.ydir end ball:translate( dx, dy ) end end Runtime:addEventListener( "enterFrame", collection );

Also, you don’t need to apologize for being new.  Its cool.  You’re posting in the newbie’s forum so we will cut you some slack for questions about basic topic. 

I am now going to actually take a look at this and try to help… give me a moment.  In the interm others can now see the issue more clearly too.

OK, I’ve looked over your code (without running it) it looks great!  It should be working. 

So, what are you seeing that is not what you want?

Note: I’ll be pasting this into an actual project in a minute so I may see the problem them.  That said, if you could tell me now that would be awesome.  

PS - Great coding style for a new user.

@roaminggamer

Sorry,my english is not so good

Here is the problem,my teacher want me to use for-loops to create 20 circle,but i try many times already and failed

thanks

Hmm… Now I have run it and see no issues.  I do notice that you only defined 3 circle parameter tables, but I assume that is just  you shortening the code.

Please tell us what you’re seeing that is wrong.  As far as I can tell the code works fine.

Here is my version of your code with 20 randomly generated circles parameter tables: 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/circles20.zip

local function genRandParams() return { radius = math.random(5,30), xdir = (math.random(1,2) == 1) and -1 or 1, ydir = (math.random(1,2) == 1) and -1 or 1, xspeed = math.random( 5, 40)/10, yspeed = math.random( 5, 40)/10, r = math.random(), g = math.random(), b = math.random(), } end local params = {} for i = 1, 20 do params[i] = genRandParams() end

One more note.  Always tell us if this is help for a school assignment first.

We will help, but knowing it is for school means we have to be careful how much help we give.  It is your job to learn it, though we do not mind giving you hints and clues.  

I only say this because many times I’ve provided significant help only to find out I was essentially doing an assignment for someone.  

PS - What is your native language?  Just curious.

@roaminggamer

WOW,this is what i want,thank you so much

@roaminggamer

this not a school assignment,just homework,and it very helpful,thanks

Because i try many times and failed,and nobody can teach me,so i come here to ask help

my native language most is chinese

PS:very thank you

 

 

 

2/5000

Zhōngwén

 

 

 

2/5000

Zhōngwén

Chinese

别客气

Mandarin I presume?  I’ll keep that in mind for future posts.

@SkyKing0225,

Welcome to the community.  Please note, while I’m happy to help and will try in a moment, that was a terrible post.

A post should have these parts:

  • Clear and Concise Subject Oriented Title
    • sorry,I’m newbie,who can help me use for-loops change display.newcircle() to create 20 circle is not a title. :)  
  • Legible and well formatted body.
    1. Always format code posts with the <> button above the posting area.  Then, re-edit them to make them neat and easy to read.
    2. When posting about a bug or issue tell us:
    • What you did
    • What you saw (the result)
    • What you expected to see
    • Why you think the result is wrong.
    1. When posting a question (like this post), … post the question in a clear sentence or paragraph so we can easily read it.  Don’t got into more detail than needed to get the idea across.  Nobody likes to read long posts.

In this case you post could have been: 

(the title) Create Circles In Loop?

Hi.  I’d like to create 20 circles using a loop and i’m having trouble doing so.  Here is the code I have so far. Can someone help me figure out what I’m doing wrong?

local function newBall( params ) local xpos = display.contentWidth\*0.5 local ypos = display.contentHeight\*0.5 local circle = display.newCircle( xpos, ypos, params.radius ); circle:setFillColor( params.r, params.g, params.b, 255 ); circle.xdir = params.xdir circle.ydir = params.ydir circle.xspeed = params.xspeed circle.yspeed = params.yspeed circle.radius = params.radius return circle end local params = { { radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 }, { radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 }, { radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 }, -- newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 } } local collection = {} -- Iterate through params array and add new balls into an array for \_,item in ipairs( params ) do local ball = newBall( item ) collection[#collection + 1] = ball end -- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua) local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX function collection:enterFrame( event ) for \_,ball in ipairs( collection ) do local dx = ( ball.xspeed \* ball.xdir ); local dy = ( ball.yspeed \* ball.ydir ); local xNew, yNew = ball.x + dx, ball.y + dy local radius = ball.radius if ( xNew \> screenRight - radius or xNew \< screenLeft + radius ) then ball.xdir = -ball.xdir end if ( yNew \> screenBottom - radius or yNew \< screenTop + radius ) then ball.ydir = -ball.ydir end ball:translate( dx, dy ) end end Runtime:addEventListener( "enterFrame", collection );

Also, you don’t need to apologize for being new.  Its cool.  You’re posting in the newbie’s forum so we will cut you some slack for questions about basic topic. 

I am now going to actually take a look at this and try to help… give me a moment.  In the interm others can now see the issue more clearly too.

OK, I’ve looked over your code (without running it) it looks great!  It should be working. 

So, what are you seeing that is not what you want?

Note: I’ll be pasting this into an actual project in a minute so I may see the problem them.  That said, if you could tell me now that would be awesome.  

PS - Great coding style for a new user.

@roaminggamer

Sorry,my english is not so good

Here is the problem,my teacher want me to use for-loops to create 20 circle,but i try many times already and failed

thanks

Hmm… Now I have run it and see no issues.  I do notice that you only defined 3 circle parameter tables, but I assume that is just  you shortening the code.

Please tell us what you’re seeing that is wrong.  As far as I can tell the code works fine.

Here is my version of your code with 20 randomly generated circles parameter tables: 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/circles20.zip

local function genRandParams() return { radius = math.random(5,30), xdir = (math.random(1,2) == 1) and -1 or 1, ydir = (math.random(1,2) == 1) and -1 or 1, xspeed = math.random( 5, 40)/10, yspeed = math.random( 5, 40)/10, r = math.random(), g = math.random(), b = math.random(), } end local params = {} for i = 1, 20 do params[i] = genRandParams() end

One more note.  Always tell us if this is help for a school assignment first.

We will help, but knowing it is for school means we have to be careful how much help we give.  It is your job to learn it, though we do not mind giving you hints and clues.  

I only say this because many times I’ve provided significant help only to find out I was essentially doing an assignment for someone.  

PS - What is your native language?  Just curious.

@roaminggamer

WOW,this is what i want,thank you so much

@roaminggamer

this not a school assignment,just homework,and it very helpful,thanks

Because i try many times and failed,and nobody can teach me,so i come here to ask help

my native language most is chinese

PS:very thank you

 

 

 

2/5000

Zhōngwén

 

 

 

2/5000

Zhōngwén

Chinese

别客气

Mandarin I presume?  I’ll keep that in mind for future posts.