How do I move this turret using a slider?

If I keep clicking on the slider, it prints this into the console:

Copyright (C) 2009-2014  C o r o n a   L a b s   I n c .         Version: 3.0.0         Build: 2014.2511 Platform: GenericAndroidDevice / x64 / 6.2 / AMD Radeon HD 7800 Series / 4.4.132 83 Compatibility Profile Context 14.501.1003.0 / 2014.2511 WARNING: display.setStatusBarMode() not supported in the simulator for GenericAn droidDevice device Warning: h:\stuff\homework\unit 14 event driven programming (using lua & corona sdk)\assignments\shooting game\main.lua:185: o.anchorX is only supported in grap hics 2.0. Your mileage may vary in graphicsCompatibility 1.0 mode Warning: h:\stuff\homework\unit 14 event driven programming (using lua & corona sdk)\assignments\shooting game\main.lua:186: o.anchorY is only supported in grap hics 2.0. Your mileage may vary in graphicsCompatibility 1.0 mode Warning: h:\stuff\homework\unit 14 event driven programming (using lua & corona sdk)\assignments\shooting game\main.lua:196: o:setTextColor() is deprecated. Use  o:setFillColor() instead 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5 169.5

Hmmpf. I feel like we’re going in circles here. Are you just copy/pasting my code without adding anything to it, or are you integrating it with what you want the code to do?

Would it be easier if I shared my screen on Skype or something?  

Sometimes I don’t know where your code needs to be put in because what you write doesn’t have the same functions as what I have.  And my College has dumped us in at the deep end with Lua.

And I put print here:
 

local function onTouch( event ) local t = event.target print(slider.y)

And the Event Listener is further down:
 

slider:addEventListener('touch', onTouch)

Sharing on Skype… Sheesh… I’m sorry, I don’t mean to be rude, but I’m afraid I’m going to leave the rest of the issue in the hands of you and others because I’m putting in way too much time and we’re not really progressing.

I jumped in to help answer a simple question with a simple math formula, but we’re now venturing into questions about implementing touch listeners and code structuring and debugging. I’d love to help, but I would just be repeating information that is already all over the forum. Sorry about that.

Read the sliderButton.y value and convert it to a percentage point value.

Your final code will most likely read something like this:

local sliderCorrectedYPosition = sliderButton.y - sliderMinimumYPositionOnScreen turret.rotation = turretOffsetValue + turretRotationScale\*(slideCorrectedYPosition)

How do I do that?

I don’t understand what I need to replace in your script, exactly.

Hmmm - it’s pretty selfexplanatory to be honest.

Your slider has a .y value right? Basically all you do is say:

turret.rotation = slider.y

in an enterframe listener (so every frame), or in a touch listener (so whenever the slider is moved).

The problem is, your rotation might change way too much or too little, so you need a value to scale the effect down or up (turretRotationScale in my example), and you might need a different starting angle for the turret so you need a value like turretOffsetVale.

You will need to test with different numbers to see what you need to use as these two values.

Try this first:

turret.rotation = slider.y

And if it goes too fast try

turret.rotation = slider.y/10

If it goes too slow try

turret.rotation = slider.y * 10

Or other values. When the speed or amount of change is right, use the offset value to make sure your turret starts at the right point when the slider is at its lowest or highest point, depending on your specific needs.

turret.rotation doesn’t exist, so I assume it has to be :

turretBarrel.rotation = slider.y/10

And what do I do with 

turretOffsetValue = turretRotationScale\*(sliderCorrectedYPosition)

?

Thanks for your help.  I know I’m a real newbie at this xD

No problem.

You can simplify things without any bad consequences.

Just try:

turretBarrel.rotation = 100 + slider.y/10

Without anything else. Change the 10 into other numbers until you are about pleased with the amount of change. Then change the 100 (maybe even into a negative number) to get the start angle of your turretBarrel right.

When I do that the turretBarrel starts off in this position:[spoiler]
cfd6bb8110.png

[/spoiler]

The code I now have looks like this:

[spoiler]

function Slider( ) --Slider back sliderBack=display.newImage('Slider back.png', -30, -80) sliderBack:scale(0.3, 0.3) --Slider slider=display.newImage('Slider.png', -15, 140) slider:scale(0.3, 0.3) yMin = 107; yMax = 233; local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t, event.id ) t.isFocus = true -- Store initial touch position on the actual object - prevents jumping when touched t.xStart = event.x - t.x t.yStart = event.y - t.y elseif t.isFocus then if "moved" == phase then print(t.yStart) t.y = event.y - t.xStart if (t.y \< yMin) then t.y = yMin end if (t.y \> yMax) then t.y = yMax end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( t, nil ) t.isFocus = false end end --This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end slider:addEventListener('touch', onTouch) --Slider control turretBarrel.rotation = 100 + slider.y/10 end&nbsp;

[/spoiler]

Hi - I don’t read other people’s code because it takes too long. Sorry. I did, however tell you to first change the “10” value to get the right amount of change, and then after that change the “100” value from my sample code to change the start angle of the barrel.

So, if the start angle is wrong, change the first value. But like I said, FIRST change the “10” value to get the change amount right, and after that the other value.

Looking at your screenshot there is an alternative that could be easier.

Do you have a “start angle” for the barrel when the slider is in the middle, and then depending on wether you move up or down does the angle change? Or is the start angle when the slider is at the bottom or top?

If the basic start angle is when the slider is in the middle, could you give me the .y value for the slider when it is in the middle?

changing the /10 does very, very little, so I set it to turretBarrel.rotation = -8 + slider.y/21.  And also, moving the slider doesn’t move the turret, it stays stationary.

Some issues:

  1. Do you want to move the turret or rotate the barrel?

  2. The BIGGER you make the 10 value, the smaller the amount of change. so slider.y/2 will have a 10 times bigger effect than slider.y/20

The slider starts in the middle, and the code to input the slider is here:

--Slider slider=display.newImage('Slider.png', -15, 140) slider:scale(0.3, 0.3)

I want to rotate the barrel to shoot at the planes.  I have previously stated that I can control the barrel with some buttons, the code for that is already in place.  But they are difficult to use, so I opted for a slider.

Okay. So the default .y position in the middle is 140 pixels.

Try this code then:

turretBarrel.rotation = 90 + (slider.y-140)*0.5

Change the 90 into whatever you want to be the starting angle of the turretBarrel and change the 0.5 value to adjust the amount of change in the rotation when you slide. Watch out: in this code the bigger you make the 0.5 value, the bigger the effect will be!

Let me know how that works for you!

Okay, I have put in the code, and the barrel is in the place I want it.  But sliding the slider still doesn’t move the barrel.

Can you debug using a print statement?

Just use:

print(slider.y) in your enterFrame or touch listener and see if the console outputs the y value - we’ll take it from there.

Like this?

slider:addEventListener('touch', onTouch, print(slider.y))

Just add the print statement in your existing touch listener under both "if event.phase == “began” and if event.pahse == “moved”