I'm new with corona/lua and i'm i can't find a solution to this thing. I'm trying to spawn a object that fall from top to down and should stop at the bottom of the screen. Then i'll create the touch event etc etc..
but for now the problem is that i recieve this error:
attempt to index global 'physics' (a nil value)
and objects ofc doesn't fall down.
here is my code:
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local buttonY = display.contentWidth * 0.02
local buttonWidth = display.contentWidth * 0.1
local buttonHeight = display.contentWidth * 0.1
background = display.newImage("graphics/background.jpg")
local localGroup = display.newGroup()
local spawnTable = {}
function spawnLattina(params)
local object = display.newImage(params.image, params.buttonX,50);
object.objTable = params.objTable;
object.index = #object.objTable+1;
object.name = "object:".. object.index;
--fisica
if params.hasBody then
object.density = params.density or 0;
object.friction = params.friction or 0;
object.bounce = params.bounce or 0;
object.isSensor = params.isSensor or false;
object.bodyType = params.bodyType or "dynamic";
print(object.density .. " Friction: ".. object.friction .."bodyType: "..object.bodyType)
physics.addBody(object, object.bodyType,
{density = object.density,
friction = object.friction,
bounce = object.bounce}
)
end
object.group = params.group or nil
object.group:insert(object)
object.objTable[object.index] = object
return object
end
for i = 1, 2 do
local spawns = spawnLattina(
{
image = "graphics/lattina.png",
objTable = spawnTable,
buttonX = math.random(50,480),
hasBody = true,
density = 0,
friction = 12,
bodyType = "static",
group = localGroup,
}
)
end
You haven't started the physics engine. Write the following lines on the top of your class:
local physics = require "physics"
physics.start()
Keep Coding.................. :)
Related
I am currently trying to working with restricting the agents to on 1/4 of the whole Manhattan map. The idea is simple and as follows;
Let us say we have 10 agents, their names being v1, v2 ... v10. I want them to move only in a specific section. Currently the method in whcih the MapBasedMovement has been made is such that it moves in all the place, but I need it to move in only specified coordinates.
Thanks,
I tried picking a specific set of coordinates i.e. (0, 0) and (700, 700). Tried to find all the MapNode that are between them and then ask the DTNNode to select only the MapNode in these coordinates. But this does not give me the correct results.
Adding code below
public Path getPath() {
Path p = new Path(generateSpeed());
MapNode curNode = lastMapNode;
MapNode prevNode = lastMapNode;
MapNode nextNode = null;
List<MapNode> neighbors;
Coord nextCoord;
assert lastMapNode != null: "Tried to get a path before placement";
// start paths from current node
p.addWaypoint(curNode.getLocation());
int pathLength = rng.nextInt(maxPathLength-minPathLength) +
minPathLength;
for (int i=0; i<pathLength; i++)
{
// neighbors = curNode.getNeighbors();
neighbors = host.possibleNodes;
Vector<MapNode> n2 = new Vector<MapNode>(neighbors);
if (!this.backAllowed)
{
n2.remove(prevNode); // to prevent going back
}
if (okMapNodeTypes != null) { //remove neighbor nodes that aren't ok
for (int j=0; j < n2.size(); ){
if (!n2.get(j).isType(okMapNodeTypes)) {
n2.remove(j);
}
else {
j++;
}
}
}
if (n2.size() == 0) { // only option is to go back
nextNode = prevNode;
}
else
{
// choose a random node from remaining neighbors
//nextNode = n2.get(rng.nextInt(n2.size()));
nextNode = host.possibleNodes.get(rng.nextInt(host.possibleNodes.size()));
System.out.println(lastMapNode);
List<MapNode> nodePath = pathFinder.getShortestPath(lastMapNode, nextNode);
for (MapNode node : nodePath)
{ // create a Path from the shortest path
p.addWaypoint(node.getLocation());
}
lastMapNode = nextNode;
prevNode = curNode;
nextCoord = nextNode.getLocation();
curNode = nextNode;
return p;
}
prevNode = curNode;
nextCoord = nextNode.getLocation();
curNode = nextNode;
p.addWaypoint(nextCoord);
}
lastMapNode = curNode;
return p;
}
Here, the parts where the host.possibleNodes have been mentioned are a method that I have made which basically tries to get the coordinates of each node and bins to a specific region. I can add that method as well if needed.
I'm trying to clone a mesh using GraphicsBuffer because its read/write option is false, and I can't change that. This is my current code:
public static bool Run(Mesh sourceMesh, out Mesh generatedMesh)
{
GraphicsBuffer sourceDataBuffer = sourceMesh.GetVertexBuffer(0);
GraphicsBuffer sourceIndexBuffer = sourceMesh.GetIndexBuffer();
var vertexCount = sourceDataBuffer.count;
var indexCount = sourceIndexBuffer.count;
byte[] sourceData = new byte[vertexCount * (sourceDataBuffer.stride / sizeof(byte))];
byte[] sourceIndex = new byte[(int)(indexCount * ((float)sourceIndexBuffer.stride / sizeof(byte)))];
sourceDataBuffer.GetData(sourceData);
sourceIndexBuffer.GetData(sourceIndex);
var attributes = sourceMesh.GetVertexAttributes();
generatedMesh = new Mesh();
generatedMesh.vertexBufferTarget |= GraphicsBuffer.Target.Raw;
generatedMesh.SetVertexBufferParams(vertexCount, attributes);
generatedMesh.SetVertexBufferData(sourceData, 0, 0, sourceData.Length);
generatedMesh.SetIndexBufferParams(indexCount, sourceMesh.indexFormat);
generatedMesh.SetIndexBufferData(sourceIndex, 0, 0, sourceIndex.Length);
generatedMesh.subMeshCount = sourceMesh.subMeshCount;
for (int i = 0; i < sourceMesh.subMeshCount; i++)
{
var subMeshDescriptor = sourceMesh.GetSubMesh(i);
generatedMesh.SetSubMesh(i, subMeshDescriptor);
}
sourceDataBuffer.Release();
sourceIndexBuffer.Release();
generatedMesh.RecalculateBounds();
return true; // No error
}
It works like a charm in my test project. But when I try to clone things in my main project, GetData(sourceData) and GetData(sourceIndex) both return arrays of 0s. What could be causing that? Could it be because of the read/write being disabled?
Edit: The problem only happens in bundles with non-readable meshes. I thought GraphicsBuffer should work with them, but it doesn't.
I'm using a Lua class to create two objects, each of which must check where the other is to determine their movement. I'm attempting to use timer.performWithDelay to have them check every second, but for some reason when I try to do this, the line
o.moveCheckTimer = timer.performWithDelay(1000, o:moveCheck, 0)
in the class constructor throws an error stating that "Function arguments are expected near ','".
I have attempted to use an anonymous function like this:
o.moveCheckTimer = timer.performWithDelay(1000, function() o:moveCheck() end, 0)
but that causes the timer of both objects to only call the function for the most recent object that was created and not for itself (also very confusing behavior, and if anyone knows why this happens I would love to learn why).
I have dug through the API and info on method calls thoroughly, but I can't seem to find anything that uses them both together, and I feel like I'm missing something.
How can I use the method call as the listener for this timer?
Here is the full constructor:
Melee = {}
Melee.__index = Melee
function Melee:new(id, name, lane, side)
local o = {}
setmetatable(o, Melee)
o.id = id
o.name = name
o.lane = lane
o.side = side
if name == "spearman" then
o.maxhp = 100
o.range = 1
o.damage = {10, 20}
o.imageName = "images/rebels/spearman.png"
else
error("Attempted to create melee unit with undefined name")
end
o.hp = o.maxhp
--Display self
o.image = display.newImageRect(mainGroup, "images/rebels/spearman.png", 80, 80)
o.image.x = 0
o.image.y = lanes[lane]
o.image.anchorY = 1
if side == 2 then
o.image.xScale = -1
o.image:setFillColor(0.8)
o.image.x = display.contentWidth - 100
end
--Move and attack
local destination = display.contentWidth
if side == 2 then
destination = 0
end
o.moving = 1
o.movement = transition.to(o.image, {x = destination, time = 30000+math.random(-200,200)})
o.moveCheckTimer = timer.performWithDelay(1000, o:moveCheck, 0)
--o.attackTimer = timer.performWithDelay(1000, self:attack, 0)
return o
end
I have created a lua file called moonclass.lua
which created a display object of a moon
i have created two instances of this class, aka two moons,
these were created in level1.lua
however when i try to rotate both moons, only the last moon that is created will rotate, regardless of which one i refer the rotation to.
moonclasss.lua below:
local moon = {}
local moon_mt = { __index = moon } -- metatable
local sprite
function moon.new(name) -- constructor
local newMoon = {
name = name or "unnamed",
}
return setmetatable( newMoon, moon_mt)
end
function moon:draw(x,y,group)
sprite = display.newImage("image/moon.png")
sprite.x = x
sprite.y = y
physics.addBody( sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )
group:insert (sprite)
end
function moon:talk() print( self.name .. " is called." )end
function moon:y(y)
sprite.y = sprite.y + y
return y
end
function moon:x(x)
sprite.x = sprite.x + x
return x
end
function moon:rotate(r)
sprite.rotation = sprite.rotation + r
return sprite
end
return moon
and level1.lua:
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
storyboard.purgeScene ("menu")
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
function scene:createScene(event)
local group = self.view
local background = display.newGroup()
local foreground = display.newGroup()
group:insert(background)
group:insert(foreground)
--BACKGROUND
backgroundimage = display.newImage("image/galaxy background.png")
foreground:insert(backgroundimage)
backgroundimage.x = display.contentWidth/2
backgroundimage.y = display.contentWidth/-1
require "starclass"
require "controls"
local moon = require ("moonclass")
local moon1 = moon.new("moon1")
moon1:draw(0,400,foreground)
local moon2 = moon.new("moon2")
moon2:draw(300,400,foreground)
moon1:talk()
moon2:talk()
local platform = require "platformclass"
platform.draw(512,200,foreground)
local platform2 = require "platformclass"
platform2.draw(50,0,foreground)
local rocket = require "rocketclass"
rocket.draw(200,600,foreground)
function gameloop(event)
--if (rockety > -1920 and rockety < 320) then
--foreground.y = rockety + 320
--end
moon2:rotate(1)
moon1:rotate(-2)
end
Runtime:addEventListener("enterFrame", gameloop)
end
-- ENTER SCENE --
function scene:enterScene (event)
--local chapter1level1 = require("scene objects").loadlevelenterscene()
end
-- EXIT SCENE --
function scene.exitScene(event)
controlthrust:removeEventListener( "touch", thrust)
controlleft:removeEventListener( "touch", rotateleft)
controlright:removeEventListener( "touch", rotateright)
Runtime:removeEventListener("enterFrame", gameloop)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
return scene
local moon = require("moonclass")
local moon2 = require ("moonclass")
These lines return the same object.
require does not load a module twice.
Second require simply returns the value that was returned by the first require.
You must define a constructor function and call it to create new object.
EDIT :
local moon = {}
local moon_mt = { __index = moon } -- metatable
function moon.new(name) -- constructor
local newMoon = {
name = name or "unnamed"
}
return setmetatable( newMoon, moon_mt)
end
function moon:draw(x,y,group)
self.sprite = display.newImage("image/moon.png")
self.sprite.x = x
self.sprite.y = y
physics.addBody( self.sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )
group:insert (self.sprite)
end
function moon:talk() print( self.name .. " is called." )end
function moon:y(y)
self.sprite.y = self.sprite.y + y
return y
end
function moon:x(x)
self.sprite.x = self.sprite.x + x
return x
end
function moon:rotate(r)
self.sprite.rotation = self.sprite.rotation + r
return self.sprite
end
return moon
I have an Oracle function that returns a record set.
I introduced parameters to the Oracle function and this is causing the front-end code to go haywire.
Here's my front-end code.
OracleCommand od = oc.CreateCommand();
od.CommandType = System.Data.CommandType.Text;
od.CommandText = " select * from table(pkg_fetchPOInfo.getPORowsTable(:1,:2))";
//od.CommandText = "pkg_fetchPOInfo.getPORowsTable";
//od.CommandType = System.Data.CommandType.TableDirect;
OracleParameter op1 = new OracleParameter();
op1.ParameterName = "1";
op1.OracleDbType = OracleDbType.Varchar2;
op1.Direction = System.Data.ParameterDirection.Input;
op1.Size = 6;
op1.Value = strPONumber;
od.Parameters.Add(op1);
OracleParameter op2 = new OracleParameter();
op2.ParameterName = "2";
op2.OracleDbType = OracleDbType.Varchar2;
op2.Direction = System.Data.ParameterDirection.Input;
op2.Size = 3;
op2.Value = "US";
od.Parameters.Add(op2);
If I execute the query in the front-end SQLPLUS, I get a recordset.
This code works if I remove the parameters from the package and the front-end code.
select * from table(pkg_fetchPOInfo.getPORowsTable('1007446','US')); --works in SQLPLUS.
select * from table(pkg_fetchPOInfo.getPORowsTable()); --works in both places.
Am I assigning the parameters incorrectly?
Package Definition:
CREATE OR REPLACE
PACKAGE TESTP AS
function TESTPIPE(nr in number, nr2 in number) return varchartabletype pipelined;
END TESTP;
CREATE OR REPLACE
PACKAGE BODY TESTP AS
function TESTPIPE(nr in number, nr2 in number) return varchartabletype pipelined AS
CURSOR TESTPIPE_cur
IS
SELECT (level + 1) datam
FROM dual
connect by level < nr;
vtt varchartabletype ;
BEGIN
OPEN TESTPIPE_cur;
LOOP
FETCH testpipe_cur
BULK COLLECT INTO vtt LIMIT nr2;
FOR indx IN 1 .. vtt.COUNT
LOOP
Pipe Row ( vtt( indx ) ) ;
END LOOP;
EXIT WHEN testpipe_cur%NOTFOUND;
END LOOP;
END TESTPIPE;
END TESTP;
.NET Code:
public static void pipeTest()
{
String conString = GetConnectionString();
OracleConnection _conn = new OracleConnection(conString);
_conn.Open();
OracleCommand oCmd = new OracleCommand();
oCmd.CommandText = "begin open :crs for Select * from table(testp.testpipe(:nr,:nr2)); end;";
oCmd.CommandType = CommandType.Text ;
oCmd.Connection = _conn;
OracleParameter crs = new OracleParameter();
crs.OracleDbType = OracleDbType.RefCursor;
crs.Direction = ParameterDirection.Output;
crs.ParameterName = "crs";
oCmd.Parameters.Add(crs);
OracleParameter nr = new OracleParameter();
nr.OracleDbType = OracleDbType.Int64;
nr.Direction = ParameterDirection.Input ;
nr.ParameterName = "nr";
nr.Value = 25;
oCmd.Parameters.Add(nr);
OracleParameter nr2 = new OracleParameter();
nr2.OracleDbType = OracleDbType.Int64;
nr2.Direction = ParameterDirection.Input;
nr2.ParameterName = "nr2";
nr2.Value = 10;
oCmd.Parameters.Add(nr2);
using (OracleDataReader MyReader = oCmd.ExecuteReader())
{
int ColumnCount = MyReader.FieldCount;
// get the data and add the row
while (MyReader.Read())
{
String s = MyReader.GetOracleValue(0).ToString();
Console.WriteLine(string.Format("i={0}", s));
}
}
Console.ReadLine();
}