Scaling embedded YouTube videos...

I posted my first blog entry with embedded video this morning. As you’d expect I simply copied the supplied embed code from the relative video on YouTube (with customised colour of course) and pasted it in my post*. As my blog text column is only 350pixels wide and YouTube’s default video width is 425pixels this meant that I had to resize the video whilst maintaining the videos width to height ratio. How? Easy...

In your YouTube embed code you will see TWO instances of:

width="425" height="344"

Simply change the BOTH the instances to the width and height you require. In my case the width="350". As the ratio of the YouTube window seems to be roughly 1.2:1 to find the height simply divide your width by 1.2

width / 1.2 = height
350 / 1.2 = 291.666 (I rounded down to 291)

Once you’ve plugged in your new numbers the video window will be nicely resized.


* Therein lay some problems leading me to have to download WLW Beta – but more on this later.

Modulus in Flash ActionScript 2...

The Modulus is the remainder of division between two integers. I was looking for this operator in Flash a little earlier. Flash CS3's help files are fairly difficult to wade through at the best of times and simply typing 'Modulus' into the search box didn't bring up any search results in Flash's help files.

I hit Google and discovered that the operator required is the % symbol:

X % Y = the remainder of X / Y

For example: 

10 % 3 = 1
10/3 = (3*3) + 1(remainder)

Keep It Up (bouncing ball physics) Lingo behaviour code...

I thought I would post the original code from the Keep It Up game from my last post. As the game evolved I added a lot more stuff such as scaling the shadow, disorting the ball when it was kicked and including 'trick kicks' (which was a bit of a nightmare). This is the basic code as version 1. Feel free to use it and add your own twist if you wish. Simply start a new Director project, drag a ball sprite onto the Stage and position a shadow sprite one channel below it in the Score. Add a simple 'go to the frame' behaviour to the Frame and attached the ball behaviour below to the ball sprite. Done!

-- jim's bouncing ball behaviour

property my, shad
property grav, accelH, accelV

on beginsprite me

   my = sprite(me.spritenum) -- sets a variable 'my' to easily refer to this sprite

   grav = 1 -- the gravity, change this to change the physics of the game

   accelH = 0 -- the acceleration in x direction
accelV = 0 -- the acceleration in y direction

   shad = sprite(my.spritenum - 1) -- sets 'shad' to the sprite i use as a shadow

   my.locv = 150 -- initial position of the ball. use (sprite(1).bottom - 20 - my.height/2) to place it on the bottom of the screen
shad.loch = my.loch -- sets the x position of the shadow to the same as the ball


on exitFrame me

   accelV = accelV + grav -- affects gravity on the y acceleration
   accelH = accelH * 0.96 -- dampens the x acceleration

   my.locv = my.locv + accelV -- affects y acceleration on the balls y position
   my.loch = my.loch + accelH -- affects x acceleration on the balls y position

   shad.loch = my.loch -- positions the shadow to the same x location as the ball (you may like to also change the size of the shadow)

   -- tells the ball what to do when it hits the left side of the screen
   if my.loch < (sprite(1).left + 3 + my.width/2) then
loch = (sprite(1).left + 3 + my.width/2)
      accelH = accelH * -
   end if

   -- tells the ball what to do when it hits the right side of the screen
   if my.loch > (sprite(1).right - 3 - my.width/2) then
      my.loch = (sprite(1).right - 3 - my.width/2)
      accelH = accelH * -
end if

   -- tells the ball what to do when it hits the top of the screen. you can add this back in if you want the ball to bounce off the top
--if my.locv < (sprite(1).top + 3 + my.height/2) then
-- my.locv = (sprite(1).top + 3 + my.height/2)
      -- accelV = accelV * -0.8
--end if

-- tells the ball what to do when it hits the bottom of the screen
if my.locv > (sprite(1).bottom - 20 - my.height/2) then
my.locv = (sprite(1).bottom - 20 - my.height/2)
      accelV = accelV * -
end if

   my.rotation = my.rotation + accelH -- rotates the ball based on the x acceleration


on mousedown me

   -- works out the x and y accelerations when the user clicks on the ball
-- the further from the centre of the ball the harder the kick
h = (the mouseh - my.loch) * -1
   v = (
the mousev - my.locv) * -1

   accelV = v
   accelH = h