PDA

View Full Version : My Barrel keeps Cloning itself!



Pleng
12-11-2010, 11:52 AM
I have a barrel to which the following code is attached:



function OnInit()
this:SetVisualProfile("~/misc.xml", "ent_barrel");
this:LoadCollisionInfo("~/ent_barrel.col");
this:SetEnableRotationPhysics(false);
this:SetGravityOverride(0);
m_Reverse=(false);
m_Speed=this:Data():GetNum("speed");
end

function OnPostInit()

this:SetRunUpdateEveryFrame(true);
this:SetCollisionCategory(C_CATEGORY_TILEPIC, false);
this:SetCollisionCategory(C_CATEGORY_ENTITY, false);
this:SetCollisionCategory(C_CATEGORY_PLAYER, false);
this:SetCollisionListenCategory(C_CATEGORY_ENTITY, true);
this:SetCollisionListenCategory(C_CATEGORY_PLAYER, true);

end

function OnAction(enemy)

end

function Update(step)

currentPos=this:GetPos();
newX=currentPos.x;

if (m_Reverse==false) then
newY=currentPos.y+m_Speed;
else
newY=currentPos.y-m_Speed;
end

this:SetPos(Vector2(newX,newY));

end

function OnCollision(vPosition, vVelocity, vNormal, depth, materialID, entity, state)

LogMsg(entity:GetName());

if (entity:GetName()=='bottom_line') then
m_Reverse=(true);
end

if (entity:GetName()=='top_line') then
m_Reverse=(false);
end

end


I can hold-click to drag it once. Then any further draggings, selections, increases in size, etc will clone the barrel rather than alter the original.

This does not happen with any other entities or tilepics on my map. What's going on?!!

Seth
12-12-2010, 08:00 AM
Hmm. I suspect it's moving while you're trying to edit it.

I think you need to add a "if the editor is active, exit now instead of doing anything" in the Update(). (I can't remember the exact way to detect if the editor is running, it's in some other scripts though, text search for editor)

Generally if you move something, you want to multiple it with the frame's delta (the var "step" that is being passed in) so timing will be right - if the game is paused, delta will be 0 and stop the moving, this also might fix it.

(this is all just a guess!)

Pleng
12-12-2010, 11:02 AM
Yup that was the problem. If (GameIsActive==fasle) was the condition I needed to check for.


function Update(step)


if (GameIsActive() == false) then
// the editor is active
// do nothing
else
currentPos=this:GetPos();
newX=currentPos.x;

if (m_Reverse==false) then
newY=currentPos.y+m_Speed;
else
newY=currentPos.y-m_Speed;
end

this:SetPos(Vector2(newX,newY));
end

end