so many problems...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

jodius.maximusGAsngK
Posts: 28
Joined: Thu Dec 08, 2011 12:20 pm

so many problems...

Post by jodius.maximusGAsngK » Wed Sep 18, 2013 12:34 am

I'm trying to make a side-scrolling platform kind of game, but nothing seems to be working properly even though I'm (usually) not getting any errors.

1) I've set up quasi gravity so the character falls to the ground, but not through it. I want the character to walk left and right to wherever the mouse was clicked and be able to travel up and down hills/slopes. I've isolated the horizontal movement but I can't figure out how to make it detect and follow the slope of the hill. Right now it just moves into/through the hill.

2a) I want the character to jump with some acceleration so this is what I tried:

Code: Select all


on mouseUp
   put true into sPlayerJump
   playerJump
end mouseUp


on playerJump
   
   if sPlayerJump is true then
      
      repeat with x = 6 to 1
         if x >= 1 then
            set the top of button "player" to the top of  button "player" - x
         else 
            put false into sPlayerJump
         end if
      end repeat
      
   else
      
      repeat with x = 1 to 6
         set the top of button "player" to the top of button "player" + x
      end repeat
      
   end if
   
   
end playerJump

The idea being that when you click, the character jumps and the change in vertical position decreases over time so the jump slows down. When the vertical change reaches zero, it reverses and the character falls, speeding up until it hits the ground.
I'm not getting any errors, but the "player" button isn't moving.

2b) How would I implement this with a swipe gesture where the character jumps in the direction/angle of the swipe? I've read the tutorials but I can't seem to get any gestures working.

3) I want to tap somewhere and have a graphic rotate to face that position. I've got it set up to find that angle and I have an answer dialog popping up to show that it is in fact returning an angle, but when I "set the angle" of the graphic it doesn't move. Again I'm not getting any errors, but the graphic doesn't do anything. I'm also getting some wonky angles like I click somewhere and it shows 75 degrees for example, then I move the mouse a few millimeters and click again and it says 1003 degrees...

Code: Select all


on mouseDown
   
   put the loc of graphic "rectangle" into tButtonLoc
   put the mouseloc into tMouseLoc
   put ((item 1 of tMouseLoc) - (item 1 of tButtonLoc)) into tAngleX
   put ((item 2 of tMouseLoc) - (item 2 of tButtonLoc)) into tAngleY
   put the ((tan of (tAngleY/tAngleX))*180/pi) into sFireAngle
   
end mouseDown

on mouseUp
   answer the round of sFireAngle
end mouseUp

Ultimately, I want to be able to tap/click and drag around the screen and have that graphic rotate to follow.

All of these samples are in separate files by the way. I'm trying to wrap my head around all of these actions before putting them together into one file.

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: so many problems...

Post by dave_probertGA6e24 » Wed Sep 18, 2013 6:10 am

Hi,

Don't have time to answer all of your questions, but I can point you in a direction.

Regarding 2a.
When you click the jump button you then call playerjump. This call happens immediately and the computer will then do all that is in the playerjump function before returning control to the user.
Therefore the repeat loops inside the function are run through and finished. The moving of the button will happen on each repeat, but the next repeat loop will happen without any delay. So you're repeat with x= 1 to 6 will not animate.

You really need to have a controlling function that is called every few milliseconds and in that you tell all the various items that might animate to move to their next position. For example when you click the jump button you send a message to the 'animation controller' that the player button should be animated. The controller will then move it every few milliseconds by some amount, let the user see the change and then call itself again.

Using a repeat loop will not achieve animation without some method of handing control back to the system so it can update the screen.

I can recommend the RunRev Game Academy stuff - it provides a good groundwork for developing 2d games with animation, etc. As I see you signed up for that before.

Hope that made some sense!

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: so many problems...

Post by Simon » Wed Sep 18, 2013 6:26 am

Actually I thought 2a was because sPlayerJump wasn't declared as a local

local sPlayerJump
on mouseUp......

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: so many problems...

Post by dave_probertGA6e24 » Wed Sep 18, 2013 6:41 am

Hi Simon,

That could be true, but even if it wasn't set to a value the 'else' state would be used. That has a repeat which would not work anyway as an animation.

Think of animation being a combination of both movement and timing. Repeat loops with no delays will not produce animation - only the end result. ;)

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: so many problems...

Post by Simon » Wed Sep 18, 2013 8:20 am

You are correct Dave.
Forgot about the "else".

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jodius.maximusGAsngK
Posts: 28
Joined: Thu Dec 08, 2011 12:20 pm

Re: so many problems...

Post by jodius.maximusGAsngK » Wed Sep 18, 2013 10:01 pm

Dave - I get what you're saying and it makes sense, I'm just not sure how to actually implement it. I don't know how to make it do the initial move amount, wait and then go back and grab the next move amount. In part 1 where I was trying to work out the player movement, I have it set up so that on updateScreen the player falls to the ground. When you click on the player button it "jumps" by moving up 50 pixels, waits 7 milliseconds, then goes back to updateScreen to fall back to the ground. That works okay, but the reason I'm trying to repeat with changing move amounts is to give the jump motion some ease in and ease out. I downloaded the trial of Animation Engine which has ease in and ease out built in, but I can't get that to work either.

Simon - I do have sPlayerJump declared as a local. I guess I should have included that in the code snippet.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: so many problems...

Post by Simon » Wed Sep 18, 2013 10:50 pm

For 1):
Check out "Intersect" function in the dictionary.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: so many problems...

Post by Simon » Wed Sep 18, 2013 11:05 pm

Here is a good resource for making games:
https://sites.google.com/a/pgcps.org/livecode/home
Lots of Intersects used in there.
I didn't immediately see any angle calculation games.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: so many problems...

Post by dave_probertGA6e24 » Wed Sep 18, 2013 11:59 pm

Hi,

In the past, when I played with making a toy 2d platformer, I had the player object (it was a group, but a button would do the same) that had some custom properties - one for up/down speed (up was positive), linked to this was an acceleration for vertical, another for direction of movement (DoM) speed, another to tell which direction DoM was in and another for the DoM acceleration. These accelerations were how far it would try to move in the next time slot (ie. frame update). Every update I would apply the surrounding forces to whichever of the directions were required - eg. Gravity added a negative value to the vertical acceleration. Ground friction was applied to the DoM speed - though this was never allowed to go negative - if the value went < 0 then the Direction was changed and the value was made positive.

The speed values are the last frame speed, acceleration is added to this and other forces are either added or subtracted. This will give a new speed for this frame. Really this should be called "distance to travel this frame" instead of speed, but I like speed 'cos it's easier to type!!!

You can enhance the range of properties to cover many other aspects quite easily - eg. drag (both vertical and horizontal), braking torque, wing friction, max speed, etc.

There are lots of things that can be done to the values. After this the new position of the player is calculated based on the speeds (mainly a simple set of additions). Collision detection with obstacles and platforms (ground!) are calculated based on the new positions and if there is a collision then the speeds would be adjusted to suit (dropping down to the ground does not necessarily mean that forward movement is also zeroed!)

The speed, acceleration and direction properties of the object are then replaced with the eventual new values.

By making the movement handler generic and passing an object id to it meant that I could have multiple moving objects all using the one function but getting their speeds from the object itself.

If you are likely to have multiple moving objects that can collide with each other then you might want to use a deferred movement system. Do the calculations of where the objects are going to be, check for all collisions with other movable objects, then apply the visual move command. This sounds complicated, but it's a better way of handling a more complicated setup ;)


Here are some examples of how to tell the player to do things:
To Jump - Set the vertical acceleration to something quite large (more than the gravity pull value)
To Walk - set the horizontal speed to a small value, with Horiz acceleration to 0
To Run - set the horizontal speed to a larger value, with Horiz acceleration to 0 (or use accel + a max speed setting - this would ease-in to a run)
To go from run to walk (or stop) - set the horizontal acceleration to negative something

I think you can get the picture from that.


Regarding 2b. To do a gesture you need to grab the touch position in one frame and then grab a new position in the next frame - then compare the two sets of values. Based on the x and y differences you can tell if the finger/mouse had moved between frames. If so then you can calculate the angle of the movement.

Re: 3. You might want to use "atan2" - it'll give better results. :)
But for the most part the problem is likely to be with Livecode not being able to set the angle of many types of graphic. Any graphic that has a points list in it (eg. Line, Polygon, etc) can't be rotated easily via an angle property. Even a regular polygon has problems!
You can probably get around this by using the revRotatePoly function - a bit less easy, but does the job ok.

I hope some of the above is useful.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: so many problems...

Post by dave_probertGA6e24 » Thu Sep 19, 2013 12:01 am

Good choice Simon.

Scroll to the bottom of that page and you will see a section called Posts - the first thing in it is my game on iTunes :) Not one that uses any physics or intersects though :)

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: so many problems...

Post by dave_probertGA6e24 » Thu Sep 19, 2013 12:06 am

PS. forgot to mention that a simple test timed update can be done with:

Code: Select all

//--- put this in a button or opencard handler
on startupGame
  myUpdateCode
end startupGame

//--- put this in the card or stack
on myUpdateCode
  //  do code here or call other functions
  //...
  send "myUpdateCode" to me in 50 milliseconds
end myUpdateCode
Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: so many problems...

Post by SparkOut » Thu Sep 19, 2013 6:47 am

Another very much more simplistic motion + gravity sampler has been around for years: In the toolbar, open "resources" and in the resource centre, scroll all the way down the left pane to sample projects and take a look at "bouncy".
It may have an approach you can adapt at least part of.

jodius.maximusGAsngK
Posts: 28
Joined: Thu Dec 08, 2011 12:20 pm

Re: so many problems...

Post by jodius.maximusGAsngK » Fri Sep 20, 2013 3:15 am

Simon - I'm using intersect in part 1 but I can only get it to intersect at 90 degree angles. So "don't fall through the ground" and "don't walk through walls" but it won't follow a slope. If I move the object above the slope and run it, it will fall and land on the slope, but if I try to move up the slope it just goes through it. If I try to move down the slope it moves horizontally into space and then drops down until it intersects again.

Dave - Those are some very interesting and intriguing ideas you've mentioned. I don't have any programming experience besides Game Academy so I don't know how to actually implement most of your suggestions. It's frustrating because I understand the concepts you're talking about and I can see their usefulness and imagine how they should work, I just don't know how to translate that into working code.
For 2b I know how to get the touch start and end points, and find the distance and angle, but the problem is similar to the one I'm having in part 1, that being that I don't know how to get the player character to move along that angle/slope.

SparkOut - That is PERFECT! There's a lot going on in that sample though so I'm still trying to figure out which parts of it are doing what, specifically that motion of clicking on something and flicking it and having it move in an arcing motion in the direction of the flick. That is exactly what I'm trying to achieve.

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: so many problems...

Post by AZSun » Fri Sep 20, 2013 6:21 am

I was afraid to chime in, but I was thinking about a simple animation that would take the guy up the slope of hills. It won't be complex enough for the walking you want, but there might be a spot where it helps.

The move command can have an image follow a graphic line even an invisible line.

Code: Select all

move image "guy" to the points of graphic "invisible_line" in 3 seconds

jodius.maximusGAsngK
Posts: 28
Joined: Thu Dec 08, 2011 12:20 pm

Re: so many problems...

Post by jodius.maximusGAsngK » Fri Sep 20, 2013 6:52 am

With that "bouncy.rev" file, it bounces the stack around screen. When you click and drag, the stack moves 1:1 with the mouse. If you move it slowly, it doesn't do much; if you flick the mouse, it shoots across the screen and bounces a lot.
So I copied the script locals, the mouse up/down/move/release and dropme to a button script. I changed the code so that it references the button rather than the stack and the card rather than the screen. It's mostly working except for one thing: rather than following the mouse 1:1 and using the speed of that movement to affect how far/fast the object flies, the mouse movement is being amplified/multiplied (possibly exponentially). Move 1mm, the button moves 5mm, move 2mm, the button moves 10mm (I'm pulling numbers out of the air just to illustrate what's happening). I hadn't changed anything about the code except button vs stack and card vs screen. I've been tinkering with it for hours but I can't get rid of that multiplication effect. I've been rearranging things and changing what's being referenced where with no luck. The only thing I haven't tried changing is the math parts which I assume is where the problem lies, but even if I make the card the same size as my screen (more or less replicating the original file) it still amplifies the mouse movement...
edit: The only thing I can think of is that something needs to be changed in the mouseMove handler, but I copied that straight over from the bounce file.

AZSun - I tried using a line but the character moves along it to one end and then reverses direction back to the other end.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”