How can i move to right by dragging the scene to the left in Corona? - drag-and-drop

Well, i want to make levels to my game like in Angry Birds. So how can i move from left to right by dragging the scene? What should i use for this in Corona? Thanks.

Insert all the images on the screen you want to drag (presumably everything besides any GUI objects) into a group.
From there write a function with a touch listener assigned to the group itself. It would look something like this, supposing you're app is iPhone landscape mode.
local function constrainMap ()
if localGroup.x < -480 then
localGroup.x = -480
elseif localGroup.x > 0 then
localGroup.x = 0
end
end
Runtime:addEventListener("enterFrame", constrainMap)
local function moveMap (event)
if event.phase == "began" then
localX = localGroup.x
elseif event.phase == "moved" then
localGroup.x = localX + (event.x - event.xStart)
end
end
localGroup:addEventListener("touch", moveMap)
In the above case the localGroup contains all visual elements and the constrainMap function is used to prevent the user from scrolling the map off the screen.

Related

When releasing two buttons in the new Input System it detects the differences

I've been creating a game that uses the input system for moving the player
_playerInput.actions["Move"].performed += x =>
{
// We're moving
_walking = true;
// Set the movement input to the value passed by the player
_movementInput = x.ReadValue<Vector2>();
// The angle that we need the player to rotate towards so the player look where he's moving and we change it to be in degree
_targetAngle = Mathf.Atan2(_movementInput.x, _movementInput.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
};
Here is my code but when I use the Arrows to move like the right and up Arrow and I want to release them it detects the smallest difference between the release of the two buttons which is near impossible to avoid (difference between release)
before releasing "_movementInput" contains (0.71, 0.71) but after release it becomes (0, 1) or (1, 0).
I want it to stay (0.71, 0.71) as it was before releasing the two buttons

Roblox Pathfinding Back and forth movement when reaching the destination requires jumping

For some reason when the destination target is placed somewhere where it requires jumping to get to my NPC is moving towards it while keep switching between moving back and forth.
Code:
local path = PathfindingService:CreatePath()
local waypoints
local currentWaypointIndex
local function followPath(destinationObject)
path:ComputeAsync(enemyNPC.HumanoidRootPart.Position, destinationObject.Position)
waypoints = {}
if path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
currentWaypointIndex = 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
error("Path not really working")
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex += 1
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
if reached and currentWaypointIndex >= #waypoints then
print("Target reached!")
animationTrack:Stop()
end
end
local function onPathBlocked(blockWaypointIndex)
if blockWaypointIndex > currentWaypointIndex then
followPath(destination)
end
end
path.Blocked:Connect(onPathBlocked)
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)
(I delted the top part of the code with some local variables because it didn't let me post the whole thing without saying my post is mostly code)
Does anybody know what am I doing wrong?
You're looking for the action in the PathWaypoint, which is an PathWaypointAction Enum. You have to determine whether the PathWaypoint is a Walk or Jump action. Currently, you're doing MoveTo() regardless of whether it has to jump or not.
So, in your code:
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
else
​humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end

ROBLOX: get a handler object in Touched function

I have a simple map with 2 gates (blue and red) and a ball.
I need a listener function for checking ball collisions with a gate.
I have created a server script:
function goal(hitter)
if hitter.Name == "ball" then
print(Instance.Parent)
print(hitter)
print("============")
end
end
game.Workspace.gate_blue.Touched:Connect(goal)
game.Workspace.gate_red.Touched:Connect(goal)
I need to detect in the function which gate has been hitted.
How can I get the gate name in the function?
One way you could reuse the goal functionality is to make a higher-order function :
function onGoalTouched(goalName)
return function(hitter)
if hitter.Name == "ball" then
print(Instance.Parent)
print(hitter, goalName)
print("============")
end
end
end
local blueGate = game.Workspace.gate_blue
local redGate = game.Workspace.gate_red
blueGate.Touched:Connect(onGoalTouched(blueGate))
redGate.Touched:Connect(onGoalTouched(redGate))

Roblox; making destroyed parts reappearing

I've made a game where you step on a specific tile this part gets destroyed. I want to make it that after a certain amount of time this destroyed block will reappear, now you might wonder why I just don't make the part invisible and make it lose it's player collision. I have not done this because I don't know how to make the Texture on top of the part transparency 1.
You could make a copy of the part, then do the destruction, and put the copy back into its place after a few seconds:
function onTouched(hit)
-- see if it was a player that touched the part
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
-- make backup of the part and its parent
local part = workspace.Part
local backup = part:Clone()
local backupParent = part.Parent
part:Destroy() -- do some cool effect for the destruction of it...
spawn(function()
wait(5)
-- put part back in place
backup.Parent = backupParent
backup.Touched:Connect(onTouched)
end)
end
workspace.Part.Touched:Connect(onTouched)
If you just want it to disappear you could also just remove it from the object tree temporarily by setting its Parent to nil:
function onTouched(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
local part = workspace.Part
local backupParent = part.Parent
part.Parent = nil
spawn(function()
wait(5)
part.Parent = backupParent
end)
end

Pygame check for collisions with the top most foreground rect

I have 10 sprites which I objects of the main sprite I wrote with different images and starting positions etc. But they all behave the same way. They are sub sprites of the main sprite.
I want to be able to hold mouse click on one's rect and move it round the screen which works perfectly fine. But the problem is they all have the same controls click and drag to move them. So if I am clicking on one of the sprite rects and I drag it over another one it picks it up as well. And I don't want that to happen.
Is there a way to only check for collisions with the top most foreground rect or if someone could explain a way of doing this that would achieve similar results. I have had a look at the rect documentation but I can't find a solution.
def update(self,):
self.move(self.rect)
def move(self,rect):
if pygame.mouse.get_pressed() == (1, 0, 0) and the_rect.collidepoint(pygame.mouse.get_pos()):
self.state = 1
elif pygame.mouse.get_pressed() == (0, 0, 0) and the_rect.collidepoint(pygame.mouse.get_pos()):
self.state = 0
if self.state == 0:
the_rect.centerx = the_rect.centerx
the_rect.centery = the_rect.centery
elif self.state == 1:
(the_rect.centerx, the_rect.centery) = pygame.mouse.get_pos()
Rather than using the pygame.mouse.get_pressed() function, use the event queue and check for a pygame.MOUSEBUTTONDOWN event. It will only get fired once when the button is first pressed.