Friday, 14 November 2014

How to Use Screen Shots in Flash

Sometimes, you want to take an exact picture of what is on your screen and put the image into Flash, but when you do, most of the image is transparent!

1) Press the PrtScn button on the keyboard to take a picture of the screen. Alternatively, to take a picture of a particular window, make sure the window is selected, hold down Alt, and then press PrtScn.

2) Open up a Word document, and press Ctrl+V (or just right-click and select Paste).

3) Select the resulting image, and copy it (right click, Copy, or Ctrl+C).

4) Now you can go back to your Flash document and paste the image, and it will look the way it should.

Have fun!

Monday, 18 August 2014

How to randomly generate -1 or +1, in one line.

A hobby of mine is avoiding if statements, with mathematical formula that return the right number based on other conditions.

It is easy to generate a 0 or a 1 randomly, simply use Math.round(Math.random());

That will work in a number of languages, or something similar.

For a random -1 or +1, try this:

var randNum = -1 + (2*Math.round(Math.random));

If the random number generated by Math.random() is 0.5 or above, then the answer will be 1. If the random number generated is less than 0.5, the answer will be 1.

Enjoy!

Sunday, 17 August 2014

How to make a RevoluteJoint in Box2D/CreateJS

There are plenty of posts on how to get box2d and createjs working, Lee Brimelow has a great youtbe video that covers how to get started very nicely.

But when I tried to make a revolute joint, I made a silly mistake that took me hours to find the source of. I'm a bit of a newbie, but I didn't find the problem to my solution online, so I thought I'd post it in case it happens to anyone else. And then maybe they can go to sleep before 5 in the morning...

Does this sound familiar?

"I can make the two objects, but when I try to create the joint, I get an 'Uncaught TypeError: undefined is not a function' message.'

Well, first, since I was working from the Lee Brimelow example as a starting point, I hadn't made sure I had setup the shortcut to the actual RevoluteJoint parts of box2d.

eg.
var box2d = {
b2Vec2 : Box2D.Common.Math.b2Vec2,
b2BodyDef : Box2D.Dynamics.b2BodyDef,
b2Body: Box2D.Dynamics.b2Body,
b2FixtureDef: Box2D.Dynamics.b2FixtureDef,
b2Fixture: Box2D.Dynamics.b2Fixture,
b2RevoluteJointDef: Box2D.Dynamics.Joints.b2RevoluteJointDef,
b2RevoluteJoint: Box2D.Dynamics.Joints.b2RevoluteJoint,
b2World: Box2D.Dynamics.b2World,
b2MassData: Box2D.Collision.Shapes.b2MassData,
b2PolygonShape: Box2D.Collision.Shapes.b2PolygonShape,
b2CircleShape: Box2D.Collision.Shapes.b2CircleShape,
b2DebugDraw: Box2D.Dynamics.b2DebugDraw
};

Still wouldn't work though. So here was the problem, my makeJoint function looked like this:
function makeJoint() {
var jDef = new box2d.b2RevoluteJointDef();
jDef.bodyB = c;
jDef.bodyA = a;
jDef.collideConnected = false;
jDef.localAnchorB = new box2d.b2Vec2(0/SCALE,80/SCALE);
jDef.localAnchorA = new box2d.b2Vec2(0,0);
j = world.CreateJoint(jDef);
}

where a (I thought) was the anchor body, and c was the other body I wanted to attach to it.

Unfortunately, a and c were actually the fixtures of those bodies, instead of the bodies themselves.

So in my function that made the anchor body, it said:
a = world.CreateBody(anchorBodyDef).CreateFixture(anchorFixtureDef) ;

So I changed it to:
a = world.CreateBody(anchorBodyDef);
a.CreateFixture(anchorFixtureDef);

Hope that helps.

Tuesday, 22 July 2014

How to rewind an animation, AS3

To make the animation rewindable, it needs to be all on one timeline for starters. With no movieclips doing anything complicated that will look weird if everything is going backwards while they are going forwards. Flashing or jiggling, or something like that is fine, but it has to work backwards and forwards.

Graphic symbols are fine, because they will play backwards as the movie clip goes backwards.

Anyway...

In order to make the clip go backwards:

clip.addEventListener(Event.EnterFrame, goBack);

function goBack(evnt:Event):void {

clip.prevFrame();

}

In order to get the clip to stop going backwards, either once it gets to the beginning, or at certain points along the way, put code on the relevant frames inside the movie clip that checks if the eventListener exists, and if it does, removes it.

I hope that was clear enough.

Friday, 4 July 2014

Flash: How to make the white transparent in a grey scale image

What if you want to get a picture and colour it in, but you don't want it to take forever, or lose any of the detail?

(Apologies if this can be done more easily in another program, but when it comes to pictures, I'm most comfortable with Flash.)

1) Find your image, the more contrast the better - ie, some really light areas and some really dark, the less grey the better.

2) Copy and paste it into flash.

3) Turn it into a movie clip, select it, and in the Properties box, select Adjust Colour, and increase the contrast. Try to choose an optimum value where you can see the details you need, but the grey areas have turned white.

4) Then choose the blending option Darken. The white areas should now be transparent.

5) Lock the layer of the photo, and create a new layer underneath. With this layer selected, draw lines to delineate the blocks of colour you want to show through the image. You can apply fills with gradients, or make areas of colour into movie clips and give them glows as ways of mixing colours etc.

6) If you want to make the lines darker, you can copy and paste the movie clip on top of itself (this way it will have the same contrast settings) and set the blending option of the one on top to Multiply.

Example:


Before

After
I'm no artist, and I'm not claiming that mine is an improvement on the original, but I am claiming that this didn't take me very long at all.

Wednesday, 29 January 2014

Arduino - Whoo!

I have in my posession, (it's not mine though) an Arduino Uno. Quite a departure from Actionscript, CreateJS etc (except I'm not departing them...)

What Is Arduino For?

I'm not quite sure yet, but it can take input from a variety of sensors, and it can connect to output, eg. lights motors etc. So although it seems quite limited in terms of memory, it seems to have a lot of potential. Anyway, I'm sure it has more potential than I'll ever be able to make use of, so, who cares what it's limits are! Also, I read somewhere that it may be able to work with Flash...

I've found some great tutorials online, the first one is here:

http://www.youtube.com/watch?v=fCxzA9_kg6s

I'm just making notes of anything extra.

I went through tutorial 1, and the first issue I came across was installing the driver. They guy in the video had a later version of Windows than me. The help on arduino.cc for XP users also didn't match what I saw on my screen.

So, I went to the Control Panel, System, Device Driver, and was faced with a long list of things... I don't know what they were. I couldn't see anything called Arduino, and I couldn't see what they suggested might be under ports either. But when I unplugged and replugged the arduino board back in, something appeared under USB. So I followed the recommended procedure (section number 4 on this page: http://arduino.cc/en/Guide/windows) with this instead. And that worked fine.

The second issue was when I tried to upload the program, actually, I think it worked the first time, but when I tried to make changes, I couldn't see any effect. In the video (the first link above), an error message came up when he had forgotten to set the serial port - which he changed but then didn't show how to do it from the menu.

When I tried to upload, I got this error message:

avrdude stk500_getsync(): not in sync resp=0x00

When I changed the com port (go to Tools, Serial Port, and then select a different com port) everything was fine after that.