Skip to content

Creating smooth left/right button controls for mobile

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

MA

MobileDev_Alex

Posted on August 1, 2024 • Intermediate

📱 Need help with smooth mobile controls

I’m creating a platformer/story game and need to add mobile support since I’m developing on mobile. The game will have both keyboard and mobile controls. I already know how to make keyboard controls, but I’m struggling with creating smooth left/right buttons that don’t feel choppy.

The main issues I’m facing:

  • Button presses feel laggy and unresponsive
  • Movement is choppy instead of smooth
  • Need both visual feedback and smooth character movement

Any help with creating professional-feeling mobile controls would be amazing! 🙏

TC

TouchControl_Expert

Replied 1 hour later • ⭐ Best Answer

Great question @MobileDev_Alex! Creating smooth mobile controls is crucial for a good user experience. Here’s a comprehensive solution that addresses all your concerns:

🎮 Step 1: Create Button Sprites

First, create two button sprites (Left Button and Right Button) with two costumes each:

  • Costume 1: Normal state (unpressed)
  • Costume 2: Pressed state (darker/highlighted)

📱 Step 2: Button Visual Feedback

For each button sprite, add this code for visual feedback:

    when flag clicked
forever
if <<touching [mouse-pointer v]?> and <mouse down?>> then
switch costume to [pressed v]
broadcast [button pressed v] // or [left pressed v] / [right pressed v]
else
switch costume to [normal v]
broadcast [button released v] // or [left released v] / [right released v]
end
end
  

🏃 Step 3: Smooth Character Movement

For your player character, implement this smooth movement system:

    when flag clicked
set [movement speed v] to [0]
set [max speed v] to [8]
set [acceleration v] to [1]
set [friction v] to [0.8]

forever
// Handle input
if <<key [left arrow v] pressed?> or <[left pressed v] = [true]>> then
change [movement speed v] by (0 - [acceleration v])
end
if <<key [right arrow v] pressed?> or <[right pressed v] = [true]>> then
change [movement speed v] by [acceleration v]
end

// Apply friction when no input
if <<<not <key [left arrow v] pressed?>> and <not <key [right arrow v] pressed?>>> and <<[left pressed v] = [false]> and <[right pressed v] = [false]>>> then
set [movement speed v] to ((movement speed) * [friction v])
end

// Limit speed
if <(movement speed) > [max speed v]> then
set [movement speed v] to [max speed v]
end
if <(movement speed) < (0 - [max speed v])> then
set [movement speed v] to (0 - [max speed v])
end

// Apply movement
change x by [movement speed v]
end
  

🔄 Step 4: Button State Management

Create variables to track button states:

    when flag clicked
set [left pressed v] to [false]
set [right pressed v] to [false]

when I receive [left button pressed v]
set [left pressed v] to [true]

when I receive [left button released v]
set [left pressed v] to [false]

when I receive [right button pressed v]
set [right pressed v] to [true]

when I receive [right button released v]
set [right pressed v] to [false]
  

✨ Step 5: Advanced Smooth Movement

For even smoother movement with acceleration and deceleration:

    // Advanced movement with ramp-up and ramp-down
when flag clicked
set [speed ramp v] to [0]
set [is moving v] to [false]

forever
set [is moving v] to [false]

if <<key [left arrow v] pressed?> or <[left pressed v] = [true]>> then
set [is moving v] to [true]
point in direction [-90]
if <[speed ramp v] < [8]> then
change [speed ramp v] by [0.5]
end
end

if <<key [right arrow v] pressed?> or <[right pressed v] = [true]>> then
set [is moving v] to [true]
point in direction [90]
if <[speed ramp v] < [8]> then
change [speed ramp v] by [0.5]
end
end

if <[is moving v] = [true]> then
move [speed ramp v] steps
else
// Decelerate when not moving
if <[speed ramp v] > [0]> then
change [speed ramp v] by [-0.3]
move [speed ramp v] steps
else
set [speed ramp v] to [0]
end
end
end
  

This creates a professional feeling with smooth acceleration and deceleration! 🚀

MA

MobileDev_Alex

Replied 2 hours later

@TouchControl_Expert This is incredible! Thank you so much! 🎉

The movement feels so much smoother now. One question - how can I add visual effects when the character moves, like dust particles or animation changes?

VE

VisualEffects_Pro

Replied 1 hour later

@MobileDev_Alex Great question! Here’s how to add visual polish:

    // Add this to your player sprite for animation
when flag clicked
forever
if <[is moving v] = [true]> then
if <[speed ramp v] > [2]> then
switch costume to [running v]
create clone of [dust particle v]
else
switch costume to [walking v]
end
else
switch costume to [idle v]
end
end

// For dust particles (separate sprite)
when I start as a clone
go to [player v]
change y by [-20]
set [ghost v] effect to [50]
repeat [10]
change y by [2]
change [ghost v] effect by [5]
end
delete this clone
  

This adds running animation and dust effects for extra polish! ✨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Want to Master Mobile Game Development?

Excellent discussion on mobile controls! For those looking to create even more advanced mobile games, our community can help you implement:

  • 📱 Multi-touch gesture controls
  • 🎮 Virtual joystick systems
  • ⚡ Performance optimization for mobile
  • 🎨 Responsive UI design

📚 Related Topics

Ready to create professional mobile games? Get expert guidance from our tutors in the Vibelf app!