I'm having an issue with linear velocity

I am a novice at Corona, and I’m trying to make it so that at random intervals between 1 and 3 seconds, little objects will appear above the screen and will have a linear velocity of 500 going downwards. However, the simulator just shows black and terminal says there was a syntax error: “function arguments expected near ‘=’”

Here is my code:

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

–Function to create objects up top
local ballSpawn = function()

local ball

ball = display.newImage( “button.png” )
ball.x = 40 + math.random( 380 ); ball.y = 0
physics.addBody( ball, { radius = 15 } )
ball:setLinearVelocity = 500
end

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 0 ) [import]uid: 82408 topic_id: 14452 reply_id: 314452[/import]

The code should be like this:

[lua]ball:setLinearVelocity=(300,0)[/lua]

or for the Y-Axis

[lua]ball:setLinearVelocity = (0,300)[/lua]

Make sure to check the API list and copy the API’s exactly.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 14452 reply_id: 53483[/import]

Btw, 300 or 500 is a little fast to see on screen.

Plus, your spawn logic is flawed. timer.performWithDelay function runs only once, so it only generates one random number, say 1.5 and all objects will spawn with 1.5 seconds intervals.

To solve this, you may want to put your timer function inside the spawn function, and put 1 as last parameter

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 )  

you should also keep the last line but change its last parameter to 1 too, it will trigger the spawn function for the first time, then spawn function recursively call itself with random intervals.

note: you should change your function definition to

local function ballSpawn()  

in order to work this way. Otherwise timer call inside the function does not work. [import]uid: 46529 topic_id: 14452 reply_id: 53491[/import]

@culutas,

I tried this, but I am still having the same syntax error, and the screen of the simulator is showing black. Just to be sure I’m doing this right, here’s my new code:

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

–Function to create objects up top
local function ballSpawn()

local ball

ball = display.newImage( “button.png” )
ball.x = 40 + math.random( 380 ); ball.y = 0
physics.addBody( ball, { radius = 15 } )
ball:setLinearVelocity = ( 0, 300 )
timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 )
end

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 ) [import]uid: 82408 topic_id: 14452 reply_id: 53644[/import]

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

local ball
local x = 10 --change to how many times you want your function to call.

–Function to create objects up top
local function ballSpawn()

ball = display.newImage( “button.png” )
ball.x = 40 + math.random( 380 ); ball.y = 0

physics.addBody( ball, { radius = 15 } )
ball:setLinearVelocity = ( 0, 300 )

end

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, x )

[/code] [import]uid: 87611 topic_id: 14452 reply_id: 53657[/import]

ok it seems everybody who gave an answer lost his mind. I didn’t look the ninjapigs’ answer so I couldn’t tell his mistake, also Lewis overlooked the mistake.

setLinearVelocity is a function, you call a function by adding “()” to the name of the function not “=”.

So you should change your code to :

ball:setLinearVelocity( 0, 300 )  

there will be no “=” sign after function name. [import]uid: 46529 topic_id: 14452 reply_id: 53660[/import]

@ninjapig123, it is such a small mistake but it is so funny you overlook it and give advice to check api list. :slight_smile: [import]uid: 46529 topic_id: 14452 reply_id: 53663[/import]

I was on my cell phone at the time and did not have access to the API list, I was just going off of memory, thus informing him to check my work.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 14452 reply_id: 53666[/import]

[lua]ball:setLinearVelocity( 0, 300 )[/lua] [import]uid: 29181 topic_id: 14452 reply_id: 53670[/import]

That’s what I have right now. [import]uid: 82408 topic_id: 14452 reply_id: 53672[/import]

I replaced my current code with this code you gave me, and now I’m getting a different syntax error, saying that there is an “unexpected symbol near ‘?’”

The odd thing is that there is no ‘?’ in that code.

To make sure everyone is up to date, here’s my current code:

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
 
local ball
local x = 1 --change to how many times you want your function to call.
 
–Function to create objects up top
local function ballSpawn()
         
        ball = display.newImage( “button.png” )
        ball.x = 40 + math.random( 380 ); ball.y = 0
 
        physics.addBody( ball, { radius = 15 } )
        ball:setLinearVelocity( 0, 300 )

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, x )
 
end
 
timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, x ) [import]uid: 82408 topic_id: 14452 reply_id: 53669[/import]

following code works as expected on build 606

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

local ball

–Function to create objects up top
local function ballSpawn()

ball = display.newCircle( -100, -100, 15 )
–ball = display.newImage( “button.png” )
ball.x = 40 + math.random( 380 ); ball.y = 0

physics.addBody( ball, { radius = 15 } )
ball:setLinearVelocity( 0, 300 )

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 )

end

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 )
[/code] [import]uid: 46529 topic_id: 14452 reply_id: 53674[/import]

Tried that and it’s still not working. It still shows “unexpected symbol near ‘?’” in terminal. Again, there is no question mark in my code:

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
 
–Function to create objects up top
local function ballSpawn()
         
        ball = display.newImage( “button.png” )
        ball.x = 40 + math.random( 380 ); ball.y = 0
 
        physics.addBody( ball, { radius = 15 } )
        ball:setLinearVelocity( 0, 300 )

timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 )
 
end
 
timer.performWithDelay( math.random( 1000, 3000 ), ballSpawn, 1 ) [import]uid: 82408 topic_id: 14452 reply_id: 53675[/import]

the simulator usually gives you a Line Number of the error.

Look at that line. If its one of these report back to us.

Larry [import]uid: 11860 topic_id: 14452 reply_id: 53677[/import]

there may be an invisible ascii character in your code, just copy my code exactly and paste it on a new text document. and test it. [import]uid: 46529 topic_id: 14452 reply_id: 53679[/import]

I made a new document and copied your code into it, but I’m still getting the syntax error where there is an unexpected symbol near the question mark. I also tried the ‘show invisibles’ thing on TextWrangler. All I saw was an inconspicuous symbol on line 4 (the line the error should have occurred on). [import]uid: 82408 topic_id: 14452 reply_id: 53707[/import]

I just found out something extremely important! I try copying literally any text off of Corona’s site and it gives me this exact same syntax error, saying that there was an unexpected symbol near ‘?’. I’ll try hand-typing some of the code you showed me and see if works. [import]uid: 82408 topic_id: 14452 reply_id: 53995[/import]

It works! I wrote the text in a different, un-corrupted file instead of straight-copying it off of the forum and it worked. Apparently copying the code off of Corona’s site brings along an unwanted bug that corrupts the file and makes it impossible to use for literally any code. Thanks for all your help, everybody! It works perfectly now, and I am extremely happy. Again, thank you! [import]uid: 82408 topic_id: 14452 reply_id: 53998[/import]

I am happy to hear you got it working finally. Can you tell me which browser you are using? it may related with your browser because I can copy an paste the code and it works. I use chrome. [import]uid: 46529 topic_id: 14452 reply_id: 54011[/import]

Sure. I use a special browser called Bumpercar. It’s designed for kids. [import]uid: 82408 topic_id: 14452 reply_id: 54462[/import]