[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## Rotating hero Started by ConveyedRex7592 Jan 31 2020 09:34 AM

13 replies to this topic
rotating rotate

Best Answer sporkfin , 01 February 2020 - 12:05 PM

I think I see the problem.  The rotation is always clockwise.

Two problems.

First, using hero:rotate( ) adds to the hero's rotation instead of setting the hero's rotation.

```-- instead of
hero:rotate( 270)
--use
hero.rotation = 270
```

Second,

I think using the variable "rotation" to track the direction of the rotation is confusing.

Perhaps you are confusing the direction of the joystick push with the direction of the rotation you want to implement?

Use "direction" instead of "rotation" to avoid confusion and then set "direction" to "up", "down", "left" or "right".

After each joystick event you are setting "rotation" to either "left" or "right" so the enterFrame function will get stuck in a loop.

"left" sets "rotation" to "right" and rotates the hero 180 degrees

"right" sets "rotation" to "left" and rotates the hero 180 degrees

"left" sets "rotation" to "right" and rotates the hero 180 degrees

"right" sets "rotation" to "left" and rotates the hero 180 degrees

"up" and "down" are never activated and, thus the alternating cycle between "left" and "right" makes the hero spin in a full 360 degrees

```local vector = Joystick.getVector()
if vector.x < -0.5 then
map.x = map.x + 2
if rotation == "up" then ---------- never activated
hero:rotate(270)
rotation = "left" ---------- always left
elseif rotation == "right" then
hero:rotate(180)
rotation = "left"---------- always left
elseif rotation == "down" then ---------- never activated
hero:rotate(90)
rotation = "left" ---------- always left
end
elseif vector.x > 0.5 then
map.x = map.x - 2
if rotation == "up" then ---------- never activated
hero:rotate(90)
rotation = "right" ---------- always right
elseif rotation == "left" then
hero:rotate(180)
rotation = "right" ---------- always right
elseif rotation == "down" then ---------- never activated
hero:rotate(270)
rotation = "right" ---------- always right
end
end
```

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

Why that doesn't work?

The hero spins around.

#### Attached Files

[TOPIC: post.html]
#2

### pixec

[GLOBAL: userInfoPane.html]
pixec
• Contributor

• 218 posts
• Corona SDK

What values do you get for vector x and y? You also seem to have a big gap there. You check for > 0.5 or < -0.5. If the values are between 0.5 and -0.5 then nothing happens.

[TOPIC: post.html]
#3

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 822 posts
• Corona SDK

Does it always spin in the same direction regardless of the joystick input?

If you can post the actual code it would be easier to investigate.  We probably need to know more about the "vector" and "map" values as well.

[TOPIC: post.html]
#4

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

So I need to post whole file?

When I walk left hero spins around.

Edit: It doesn't let me upload whole lua file

[TOPIC: post.html]
#5

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

I tried this. It works, but only once.

#### Attached Files

[TOPIC: post.html]
#6

### pixec

[GLOBAL: userInfoPane.html]
pixec
• Contributor

• 218 posts
• Corona SDK

thats the same code as before, so no help. just post the code using code formatting tools here, <>. Like what is ”joystick.isActivated”? How can we know your custom code does if we dont see it?

[TOPIC: post.html]
#7

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

```local composer = require "composer"
local joystickPlugin = require "plugin.joystick"
local scene = composer.newScene();
function scene:show(event)
local sceneGroup = self.view;
system.activate("multitouch")
local map = display.newImage("Images/survivalModeMap.png")
map.x = display.contentCenterX
map.y = display.contentCenterY
local hero = display.newImage("Images/mehaHat.png")
hero.x = display.contentCenterX
hero.y = display.contentCenterY
local AttackButton = display.newImage("Images/mehaAttackButton.png")
AttackButton.x = 480
AttackButton.y = 270
local rotation = "up"
local AttackAnimationSheetData = {
width = 400,
height = 400,
numFrames = 10,
sheetContentWidth = 800,
sheetContentHeight = 2000
}
local AttackAnimationSheet = graphics.newImageSheet("Images/mehaAttackAnimation.png", AttackAnimationSheetData)
local sequenceData = {
{ name = "Attack", start = 1, count = 10, time = 500, loopCount = 1}
}
local props = {
x = 10,
y = 270,
}
local Joystick = joystickPlugin.newJoystick(props)
Joystick.background:setFillColor(0.5)
Joystick.movedStick:setFillColor(0.7)
local function enterFrame(event)
if Joystick.isActivated() then
local vector = Joystick.getVector()
if vector.x < -0.5 then
map.x = map.x + 2
if rotation == "up" then
hero:rotate(270)
rotation = "left"
elseif rotation == "right" then
hero:rotate(180)
rotation = "left"
elseif rotation == "down" then
hero:rotate(90)
rotation = "left"
end
elseif vector.x > 0.5 then
map.x = map.x - 2
if rotation == "up" then
hero:rotate(90)
rotation = "right"
elseif rotation == "left" then
hero:rotate(180)
rotation = "right"
elseif rotation == "down" then
hero:rotate(270)
rotation = "right"
end
end
if vector.y < -0.5 then
map.y = map.y + 2
rotation = down
elseif vector.y > 0.5 then
map.y = map.y - 2
rotation = up
end
end
end
local AttackCoolDown = 0
local function Attack(event)
if(event.phase == "began") then
if AttackCoolDown == 0 then
hero.isVisible = false
local mehaAttackAnimation = display.newSprite( AttackAnimationSheet, sequenceData )
mehaAttackAnimation.x = display.contentCenterX
mehaAttackAnimation.y = display.contentCenterY
mehaAttackAnimation:play()
AttackCoolDown = 1
local function mehaAttackAnimationFinish ()
hero.isVisible = true
mehaAttackAnimation:removeSelf()
AttackCoolDown = 0
end
timer.performWithDelay( 500, mehaAttackAnimationFinish )
end
end
end
end
return scene;
```

[TOPIC: post.html]
#8

### pixec

[GLOBAL: userInfoPane.html]
pixec
• Contributor

• 218 posts
• Corona SDK

Better read that plugin’s documentation. Adding a few empty lines to make the code easier to read would help too.

[TOPIC: post.html]
#9

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 822 posts
• Corona SDK

Which joystick plugin are you using?

Can you tell us a little more about the input values and what you are doing with the joystick when the hero spins?

[TOPIC: post.html]
#10

### XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
• Corona Geek

• 1,255 posts
• Corona SDK

Generally when you are trying to debug your code, you'll want to use a lot of print().

For instance, you said that your your function works, but it works only once. So, insert a print to the top of your function to see whether the issue is with Joystick.isActivated() or something else, i.e.

```local function enterFrame(event)
print( joystick.isActivated() )
if Joystick.isActivated() then
...
```

Since you run enterFrame every frame (you should consider renaming the function), you'll see your console output be filled with true or false prints. If you get true once and then just false, then you know that your issue if with the first condition and you need to figure out what is causing it (probably by reading the documentation for the plugin).

If you don't get false, then it means that the issue is further down, so you'd use print again:

```local function enterFrame(event)
if Joystick.isActivated() then
local vector = Joystick.getVector()
print( vector.x, vector.y )
...
```

Again, you'll be able to see if you get values that satisfy the conditions below. You then repeat this process until you find the culprit.

Now, in your case, I would wager a guess that the issue has something to do with rotation variable. Your conditions for when rotations are done are for if the rotation is a string "left", "right", "up" or "down". However, the only values that you assign to them are "left" or "right" for x vector statements, or variables up and down for y vector, i.e. rotation is probably nil after that. How can we be sure? By using print()!

Welcome to Corona and game development, now it's time to start learning how to debug your code!

• sporkfin, pixec and ConveyedRex7592 like this

[TOPIC: post.html]
#11

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 822 posts
• Corona SDK

I think I see the problem.  The rotation is always clockwise.

Two problems.

First, using hero:rotate( ) adds to the hero's rotation instead of setting the hero's rotation.

```-- instead of
hero:rotate( 270)
--use
hero.rotation = 270
```

Second,

I think using the variable "rotation" to track the direction of the rotation is confusing.

Perhaps you are confusing the direction of the joystick push with the direction of the rotation you want to implement?

Use "direction" instead of "rotation" to avoid confusion and then set "direction" to "up", "down", "left" or "right".

After each joystick event you are setting "rotation" to either "left" or "right" so the enterFrame function will get stuck in a loop.

"left" sets "rotation" to "right" and rotates the hero 180 degrees

"right" sets "rotation" to "left" and rotates the hero 180 degrees

"left" sets "rotation" to "right" and rotates the hero 180 degrees

"right" sets "rotation" to "left" and rotates the hero 180 degrees

"up" and "down" are never activated and, thus the alternating cycle between "left" and "right" makes the hero spin in a full 360 degrees

```local vector = Joystick.getVector()
if vector.x < -0.5 then
map.x = map.x + 2
if rotation == "up" then ---------- never activated
hero:rotate(270)
rotation = "left" ---------- always left
elseif rotation == "right" then
hero:rotate(180)
rotation = "left"---------- always left
elseif rotation == "down" then ---------- never activated
hero:rotate(90)
rotation = "left" ---------- always left
end
elseif vector.x > 0.5 then
map.x = map.x - 2
if rotation == "up" then ---------- never activated
hero:rotate(90)
rotation = "right" ---------- always right
elseif rotation == "left" then
hero:rotate(180)
rotation = "right" ---------- always right
elseif rotation == "down" then ---------- never activated
hero:rotate(270)
rotation = "right" ---------- always right
end
end
```

• Michael Flad, XeduR @Spyric and pixec like this

[TOPIC: post.html]
#12

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

Which joystick plugin are you using?

Can you tell us a little more about the input values and what you are doing with the joystick when the hero spins?

I am using "Typler's Joystick"

When I wrote rotation(direction) values without inverted commas and used joystick to go left hero spun.

Now I wrote rotation(direction) values with inverted commas and when I use joystick to go left hero turns left once even if I use joystick to go right it doesn't turn.

[TOPIC: post.html]
#13

### XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
• Corona Geek

• 1,255 posts
• Corona SDK

When I wrote rotation(direction) values without inverted commas and used joystick to go left hero spun.

Now I wrote rotation(direction) values with inverted commas and when I use joystick to go left hero turns left once even if I use joystick to go right it doesn't turn.

You need to read what we've told you.

When you say that you removed the inverted commas, what you did is you turned up and down into variables. However, you haven't defined such variables anywhere, so they are nil. So, like I told you, once you set rotation, the variable, to nil once, then none of your if statements will ever work again because they are expecting strings and you are offering it nil.

Now, the second part about your hero just spinning around was explained by @sporkfin. You are using object:rotate(90) method. This method adds 90 degrees to the object's existing rotation, which means that they will only ever spin in one direction with your code. If instead you were to use object.rotation = 90, then the object's rotation will always be set to 90 degrees with it.

-

Finally, is whatever you are doing even necessary? What do you need the rotation variable for?

If all that you are doing is making your character face the direction of the joystick, then as your joystick seems to return vectors, you could use a little bit of trigonometry to manage all possible angles (see https://www.mathsisfun.com/geometry/unit-circle.html).

If you only want your character to face left, right, up or down, then you could still just monitor for the direction of the joystick. If it points left, set the character's rotation to a specific angle, etc.

• pixec likes this

[TOPIC: post.html]
#14

### ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
• Enthusiast

• 39 posts
• Corona SDK

Thanks for help sporkfinXeduR @Spyric and pixec!

[topic_controls]

[/topic_controls]