Move And Jump

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

Post Reply
problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Move And Jump

Post by problème » Sat Nov 21, 2015 5:42 pm

Hello,
I have a graphic rectangle (image the rectangle is a dog ^^) that i can move from right or left.

Code: Select all

on arrowKey direction
   movePerso direction
end arrowKey

command movePerso pDirection
   if pDirection is "right" then
      set the right of graphic "rectangle" to (the right of graphic "rectangle"" +10)

   else if pDirection is "left" then
      set the left of the graphic "rectangle" to (the left of  graphic "rectangle" - 10)
   end if

   --if the left of graphic "rectangle" < 0 then
      --set the left of the graphic "rectangle" to 0
   --end if 
end movePerso
and my rectangle can jump :

Code: Select all

on keyDown touche
   if touche is space then
      Jump
   --else 
     -- put touche
   end if
end keyDown

command Jump
   put 0 into varUp
   repeat until varUp = 5
      wait 25 milliseconds
      set  the top of graphic "rectangle" to (the top of graphic "rectangle" -  20)
      add 1 to varUp
   end repeat
   wait 100 milliseconds
   put 0 into varDown
   repeat until varDown = 5
      wait 75 milliseconds
      set  the top of graphic "rectangle" to (the top of graphic "rectangle" +  20)
      add 1 to varDown
   end repeat
end Jump
I don't know how i can do for move the rectangle at the same time jump or jump and move.
Last edited by problème on Sat Nov 21, 2015 9:57 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Move And Jump

Post by dunbarx » Sat Nov 21, 2015 9:28 pm

Hi.

Do you mean to jump, say, to the right, instead of just straight up and down?

You already know how to do this. Use the "location", not just "lefts" and "tops". Now you will add an offset to two coordinates, not just one.

See?

Craig

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Move And Jump

Post by capellan » Sat Nov 21, 2015 9:49 pm

You could check the code of this mini-game for more ideas:
http://livecodeshare.runrev.com/stack/3 ... -Game-Test

Using the Project Browser (LC IDE>Menu Tools>Project Browser),
you could see a detailed view of all scripts, images and controls
of this mini-game.

Alejandro

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Move And Jump

Post by problème » Sat Nov 21, 2015 11:28 pm

I can't understand how do I proceed because when the command Jump is using , i don't know how know if the key left or right is using.
(i don't undersand the code in the mini-game ^^)

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Move And Jump

Post by Newbie4 » Sun Nov 22, 2015 2:49 am

To jump and move or to jump in the direction that you were moving, you have a number of options.

1. When your dog (rectangle) moves left or right, save that in a global. Then when the dog jumps, also move it in the same direction as the last move. In your jump code when you move him up, do a "movePerso pDirection" and when you move him down, do another "movePerso pDirection" that will have him jump and move in the same direction he was moving in.

2. Instead of using "set the top..." Get the current loc of your graphic, and add a value to each axis (+ or - 10 to the x value and 20 to the y value) then use a "set the loc..." with the new x and y values

3. For smoother motion, use a "move..." instead of " set the loc...".

Experiment, play around with each of those and see which you prefer
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Move And Jump

Post by capellan » Sun Nov 22, 2015 3:22 am

Hi,
problème wrote:I can't understand how do I proceed because when the command Jump is using , i don't know how know if the key left or right is using. (i don't undersand the code in the mini-game ^^)
The best way to understand source code is
printing it in paper and add explanatory
and meaningful comments to it. :)

Of course, you could do exactly
the same thing in the computer.

My personal preference is to keep working
and relieve eye strain, wherever I get
the chance... :)

http://www.theatlantic.com/health/archi ... es/263005/

Alejandro

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Move And Jump

Post by problème » Sun Nov 22, 2015 1:52 pm

Hello,
I do this but, it don't really work. I don't know how i know if the rectangle is moving for do a normal jump vertical without direction. And when i do a jump with direction, during the jump i can't do right of left

Code: Select all

global speed, speedJump, direction, isMove

on arrowKey d
   movePerso d
end arrowKey

on keyDown touche
   if touche is space then
      movePerso "Jump"
   end if
end keyDown

on movePerso pDirection
   if pDirection is "right" then
      move graphic "Rectangle" relative speed,0 without waiting
      put  "right" into direction
   else if pDirection is "left" then
      move graphic "Rectangle" relative -speed,0 without waiting
      put  "left" into direction
   end if
   put true into isMove
end movePerso

on Jump
   put 0 into varS
   repeat until varS = 5
      wait 25 milliseconds
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" -  speedJump)
      movePerso direction
      add 1 to varS
   end repeat
   wait 100 milliseconds
   put 0 into varD
   repeat until varD = 5
      wait 75 milliseconds
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" +  speedJump)
      movePerso direction
      add 1 to varD
   end repeat
end Jump



Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Move And Jump

Post by Newbie4 » Sun Nov 22, 2015 3:24 pm

When code is not working as you expect, it is good to learn to "debug" your code. In most cases, the value of some variable is not what you expect it to be or you are not even getting into your code.

With most languages, you put print statements in your code to see what is happening. In the case of LiveCode, you insert "answer" statements throughout your code to see what is actually happening. In your case add statements like "answer speed", "answer direction", 'answer "in jump routine" ', etc. That way you can see where you are going and what the values are.

The better way is to get familiar with LiveCode's debugger. You can set breakpoints, look at all your variables and even step through your program one line at a time.

Try it and see if you can find the problem. If you can't, I will tell you the answer. (hint: your code is good)
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Move And Jump

Post by problème » Sun Nov 22, 2015 3:59 pm

Sorry, I changed some lines but i forgot to edit my message ^^
So the code work but during the jump i can't move left or right, i have wait the jump finish for choose a direction

Code: Select all

global speed, speedJump, direction, isMove
global moveR, moveL

on preOpenCard
   set the location of graphic "Rectangle" to 169,303
   put 10 into speed
   put  20 into speedJump
   
   put "right" into moveR
   put "left" into moveL
   
end preOpenCard

on arrowKey d
   movePerso d
end arrowKey

on keyDown touche
   if touche is space then
      Jump
   --else 
     -- put touche
      --movePerso touche
   end if
end keyDown

on movePerso pDirection
   if pDirection is moveR then
      move graphic "Rectangle" relative speed,0 without waiting
      put  moveR into direction
   else if pDirection is moveL then
      move graphic "Rectangle" relative -speed,0 without waiting
      put  moveL into direction
   end if

end movePerso

on Jump
   put 0 into varS
   repeat until varS = 5
      wait 25 milliseconds
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" -  speedJump)
      movePerso direction
      add 1 to varS
   end repeat
   wait 100 milliseconds
   put 0 into varD
   repeat until varD = 5
      wait 75 milliseconds
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" +  speedJump)
      movePerso direction
      add 1 to varD
   end repeat
end Jump

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Move And Jump

Post by Newbie4 » Sun Nov 22, 2015 4:48 pm

Your biggest problem are the waits in your code. Can you remove them, or make them smaller?

I also do not use the "move" command in games because in past versions of LiveCode, it tended to be slower. I have not tested it in the latest versions of LiveCode. There have been some major code changes in LiveCode so whatever specific advice that I give you may not necessarily apply in the version that you are using.

Otherwise, try different things, experiment. Try using different commands. Often there are different ways to code the same functions and some commands/ways are faster than others.

Finally see where you can simplify your code. That sometimes helps.

You are doing good.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Move And Jump

Post by problème » Sun Nov 22, 2015 8:43 pm

I try to do this, it's work, i can move during the jump.
I want to do too a normal jump without move. I can do this when the player press space key + a arrow key up or down. but if the player want to press only space key how can i do this ?

Code: Select all

global speed, speedJump, direction --, isDogMove
global moveR, moveL
global _jumpUp
global _jumpDown
global _isJump

on preOpenCard
   set the location of graphic "Rectangle" to 169,303
   put 10 into speed
   put 35 into speedJump
   
   put "right" into moveR
   put "left" into moveL
 
   put 0 into _jumpUp
   put 0 into _jumpDown
   put false into _isJump
end preOpenCard

on arrowKey d
   put d into direction
   movePerso d
end arrowKey

on keyDown touche
   if touche is space then
      put _isJump
      if _isJump is false then 
         Jump
      end if 
   else 
      movePerso touche
   end if
end keyDown

on movePerso pDirection
   if pDirection is moveR then
      set the location of graphic "Rectangle" to (item 1 of the location of graphic "Rectangle" + speed, item 2 of the location of graphic "Rectangle")
      --put  moveR into direction
   else if pDirection is moveL then
       set the location of graphic "Rectangle" to (item 1 of the location of graphic "Rectangle" - speed, item 2 of the location of graphic "Rectangle")
      --put  moveL into direction
   end if
end movePerso

command JumpUp
   put true into _isJump 
   movePerso direction
   if _jumpUp <= 5 then
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" -  speedJump)
      send "JumpUp" to me in 25 milliseconds 
      add 1 to _jumpUp
   else 
      put 0 into _jumpUp 
   end if 
end JumpUp


command JumpDown
   put true into _isJump 
   movePerso direction
   if _jumpDown <= 5 then
      set  the location of graphic "rectangle" to (item 1 of the location of graphic "Rectangle", item 2 of the location of graphic "Rectangle" +  speedJump)
      send "JumpDown" to me in 75 milliseconds 
      add 1 to _jumpDown
   else 
      put 0 into _jumpDown
      put false into _isJump 
   end if 
end JumpDown


on Jump
   JumpUp
   wait 100 milliseconds 
   JumpDown
end Jump

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Move And Jump

Post by Newbie4 » Sun Nov 22, 2015 10:45 pm

How will the program know when to jump in place or jump and move?

Why not make the up arrow be a jump in place and the space bar be a jump and move?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Move And Jump

Post by problème » Mon Nov 23, 2015 9:38 am

Hello,

I think i use the up arrow it is easier indeed ^^.

The problem I asked myself with the jump was this one : i will use the down arrow key for put a image "image2" which look like dog's head which look at the floor ( it hang his head), and i have a image "image1" when the dog do nothing.
So when i press down key -> i put image 2.
I want to do, when the down key was not press anymore -> put image 1

Post Reply