I am using greescock code for as3 and I simply want to annimate a movie from a class. The movie is called content in the About class where this code it.
As you can see my content is called "content" and I am replacing the mc var here with content. Hoping this would work but nothing.
Any ideas how to use Greensock as3 and how to scroll content?
var mc:Sprite = getChildByName("content") as MovieClip;
package com.views
{
import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import flash.events.MouseEvent;
import flash.text.*;
import flash.display.*;
TweenPlugin.activate([ThrowPropsPlugin]);
public class viewAbout extends MovieClip
{
public function viewAbout()
{
var bounds:Rectangle = new Rectangle(0,100,1080,1920);
var mc:Sprite = getChildByName("content") as MovieClip;
//setupTextField(mc, bounds);
var blitMask:BlitMask = new BlitMask(mc,bounds.x,bounds.y,bounds.width,bounds.height,false);
var t1:uint,t2:uint,y1:Number,y2:Number,yOverlap:Number,yOffset:Number;
blitMask.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
TweenLite.killTweensOf(mc);
trace("DOWN DOWN mouse");
//blitMask.alpha = .3;
y1 = y2 = mc.y;
yOffset = this.mouseY - mc.y;
yOverlap = Math.max(0,mc.height - bounds.height);
t1 = t2 = getTimer();
mc.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
mc.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
function mouseMoveHandler(event:MouseEvent):void
{
trace("Move Event");
var y:Number = this.mouseY - yOffset;
//if mc's position exceeds the bounds, make it drag only half as far with each mouse movement (like iPhone/iPad behavior)
if (y > bounds.top)
{
trace("Somethign ?");
mc.y = (y + bounds.top) * 0.5;
}
else if (y < bounds.top - yOverlap)
{
mc.y = (y + bounds.top - yOverlap) * 0.5;
}
else
{
mc.y = y;
}
blitMask.update();
var t:uint = getTimer();
//if the frame rate is too high, we won't be able to track the velocity as well, so only update the values 20 times per second
if (t - t2 > 50)
{
y2 = y1;
t2 = t1;
y1 = mc.y;
t1 = t;
}
event.updateAfterEvent();
}
function mouseUpHandler(event:MouseEvent):void
{
trace("UP UP UP! ");
mc.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
mc.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
var time:Number = (getTimer() - t2) / 1000;
var yVelocity:Number = (mc.y - y2) / time;
ThrowPropsPlugin.to(mc, {throwProps:{
y:{velocity:yVelocity, max:bounds.top, min:bounds.top - yOverlap, resistance:300}
}, onUpdate:blitMask.update, ease:Strong.easeOut
}, 10, 0.3, 1);
}
// constructor code
}
}
}
I found a way to put it in a function and send a var as the name of the mc I want to control. So far this works great. I am sure someone from greensock might say I am doing something that I should not but here goes.
Note my var screenX is a var from the stage.StageWidth
and the menuBanner.width is just moving the mc down on the screen.
Keep this in mind as you use this function. Hope it helps someone.
Need the proper imports:
import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import flash.events.MouseEvent;
import flash.text.*;
import flash.display.*;
TweenPlugin.activate([ThrowPropsPlugin]);
To call it use:
throwIt(yourMC);
public function throwIt(clipContent:MovieClip)
{
var bounds:Rectangle = new Rectangle(screenX - clipContent.width,0,1080,1920);
//var mc:Sprite = clipContent.getChildByName("content") as MovieClip;
var mc:Sprite = clipContent as MovieClip;
addChild(mc);
//some variables for tracking the velocity of mc
var t1:uint,t2:uint,y1:Number,y2:Number;
mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
TweenLite.killTweensOf(mc);
y1 = y2 = mc.y;
t1 = t2 = getTimer();
mc.startDrag(false, new Rectangle(bounds.x, -99999, 0, 99999999));
mc.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
mc.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
function enterFrameHandler(event:Event):void
{
//track velocity using the last 2 frames for more accuracy
y2 = y1;
t2 = t1;
y1 = mc.y;
t1 = getTimer();
}
function mouseUpHandler(event:MouseEvent):void
{
mc.stopDrag();
mc.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
mc.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
var time:Number = (getTimer() - t2) / 1000;
var yVelocity:Number = (mc.y - y2) / time;
var yOverlap:Number = Math.max(0,mc.height - bounds.height);
ThrowPropsPlugin.to(mc, {ease:Strong.easeOut, throwProps:{y:{velocity:yVelocity, max:bounds.top+menuBanner.height, min:bounds.top - yOverlap, resistance:100}}}, 3, 0.25, .3);
}
}
Related
I have generated the XML calling button into a movie clip.
Here Is Code
import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import flash.events.MouseEvent;
import flash.text.*;
import flash.display.*;
TweenPlugin.activate([ThrowPropsPlugin]);
var Buttons = new Button_mc();
var across: int = 2;
function generatebtn() {
for (var i = 0; i < buttonno; i++) {
Buttons = new Button_mc();
Buttons.name = "Button" + i;
Buttons.TypeofQuestions.text = Gk_mc.storeQuesType[i];
var row: int = Math.floor(i / across);
var col: int = i % 2;
MyMovieClip.mc_2.addChild(Buttons);
Buttons.x = col * (Buttons.width) + 25;
Buttons.y = row * (Buttons.height);
Buttons.buttonMode = true;
Buttons.addEventListener(MouseEvent.CLICK, accessclicking);
}
function accessclicking(e: Event): void {
trace(e.currentTarget.name);
}
}
generatebtn();
It's properly working with button clicking Event.
When I add more code the buttons clicking Function doesn't work. See below code:
// ----- set up masking boundary for list ------ //
var bounds: Rectangle = new Rectangle(0, 0, MyMovieClip.mc_2.width, 600);
var blitMask: BlitMask = new BlitMask(MyMovieClip.mc_2, bounds.x, bounds.y, bounds.width, bounds.height, false);
var t1: uint, t2: uint, y1: Number, y2: Number, yOverlap: Number, yOffset: Number;
blitMask.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event: MouseEvent): void {
TweenLite.killTweensOf(MyMovieClip.mc_2);
y1 = y2 = MyMovieClip.mc_2.y;
yOffset = this.mouseY - MyMovieClip.mc_2.y;
yOverlap = Math.max(0, MyMovieClip.mc_2.height - bounds.height);
t1 = t2 = getTimer();
MyMovieClip.mc_2.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
MyMovieClip.mc_2.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
function mouseMoveHandler(event: MouseEvent): void {
var y: Number = this.mouseY - yOffset;
// ----- mc's position exceeds the bounds, make it drag only half as far with each mouse movement (like iPhone/iPad behavior) ------ //
if (y > bounds.top) {
MyMovieClip.mc_2.y = (y + bounds.top) * 0.5;
} else if (y < bounds.top - yOverlap) {
MyMovieClip.mc_2.y = (y + bounds.top - yOverlap) * 0.5;
} else {
MyMovieClip.mc_2.y = y;
}
blitMask.update();
var t: uint = getTimer();
// -----If the frame rate is too high, we won't be able to track the velocity as well, so only update the values 20 times per second ------ //
if (t - t2 > 50) {
y2 = y1;
t2 = t1;
y1 = MyMovieClip.mc_2.y;
t1 = t;
}
event.updateAfterEvent();
}
function mouseUpHandler(event: MouseEvent): void {
MyMovieClip.mc_2.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
MyMovieClip.mc_2.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
var time: Number = (getTimer() - t2) / 1000;
var yVelocity: Number = (MyMovieClip.mc_2.y - y2) / time;
ThrowPropsPlugin.to(MyMovieClip.mc_2, {
throwProps: {
y: {
velocity: yVelocity,
max: bounds.top,
min: bounds.top - yOverlap,
resistance: 300
}
},
onUpdate: blitMask.update,
ease: Strong.easeOut
},
10, 0.3, 1);
}
/* Drag and Drop
Makes the specified symbol instance move-able with drag and drop.
*/
MyMovieClip.mc_2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event: MouseEvent): void {
MyMovieClip.mc_2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event: MouseEvent): void {
MyMovieClip.mc_2.stopDrag();
}
Scrolling works properly but I want to do scrolling via clicking buttons It's a long-time problem 6 months ago, but it's still not solved.
I'm going to assume your buttons are below the blitMask.
You can make mouse events fall through any DisplayObject by settings its mouseEnabled property to false
blitMask.mouseEnabled = false;
I am trying to develop a basic Snake game that involves some inverse kinematics and state machines. I'm trying to get it so when the first segment of the snake interacts with a certain "mouse", it disappears. However, when I do, it doesn't work and I end up getting ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
Here's my code:
package
{
import agent.Agent;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Point;
public class Main extends Sprite
{
private var Agents:Vector.<Agent>;
private var segments:Array;
private var numSegments:uint = 150;
private var player:Point = new Point (15, 15)
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
segments = new Array();
for(var i:uint = 0; i < numSegments; i++)
{
var segment:Segment = new Segment (5, 10);
addChild(segment);
segments.push(segment);
}
//updatePoint();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// entry point
graphics.beginFill(0xeeeeee);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
Agents = new Vector.<Agent>();
addEventListener(Event.ENTER_FRAME, gameloop);
for (var x:int = 0; x < 10; x++)
{
var a:Agent = new Agent();
addChild(a);
Agents.push(a);
a.x = Math.random() * 10;
a.y = Math.random() * 10;
}
stage.addEventListener(MouseEvent.CLICK, createAgent);
}
private function createAgent(e:MouseEvent):void
{
var a:Agent = new Agent();
stage.addChild(a);
Agents.push(a);
a.x = mouseX;
a.y = mouseY;
}
private function gameloop(e:Event):void
{
for each (var a: Agent in Agents) {
a.update();
trace ("Follow me on Twitter.");
for each(var target: Segment in segments)
{
if (target.hitTestPtarget.x, a.y + target.y, true))
{
stage.removeChild(a);
}
}
}
}
private function onEnterFrame(event:Event):void
{
drag(segments[0], player.x, player.y);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
for(var i:uint = 1; i < numSegments; i++)
{
var segmentA:Segment = segments[i];
var segmentB:Segment = segments[i - 1];
drag(segmentA, segmentB.x, segmentB.y);
}
}
private function keyDown (evt: KeyboardEvent): void {
//87=w 68=d 83=s 65=a
if (evt.keyCode == 87)
{
player.y-=50;
}
else if (evt.keyCode == 83)
{
player.y+=50;
}
else if (evt.keyCode == 68)
{
player.x+=50;
}
else if (evt.keyCode == 65)
{
player.x-=50;
}
trace (player.x + " " + player.y);
}
private function drag(segment:Segment, xpos:Number, ypos:Number):void
{
var dx:Number = xpos - segment.x;
var dy:Number = ypos - segment.y;
var angle:Number = Math.atan2(dy, dx);
segment.rotation = angle * 180 / Math.PI;
var w:Number = segment.getPin().x - segment.x;
var h:Number = segment.getPin().y - segment.y;
segment.x = xpos - w;
segment.y = ypos - h;
}
}
}
I have tried looking at other posts with the same topic, but I just don't understand what I'm doing wrong. Any help would be greatly appreciated!
You are adding Agents and Segments locally (in Main) and later you are trying to remove them from the stage (which is the top level of the swf and is an another display object).
Either do stage.addChild(...) in your init function or (this is a better option imo) replace stage.removeChild(...) with a removeChild(...) everywhere else - this will keep the objects in a local Main space (who knows, maybe you would want to move around your Main later, make everything invisible at once etc).
In my flash-made game, if my character jump on top of an enemy movieclip it spawns 3 minions from another class by MovieClip(root).addChild(spawn1);. In my minions class I've put the code for them to fall and stop upon hitting the ground and also follow my character.
I have a VCam(virtual camera) movieClip to follow my character(who moves on the stage, not the stage around him) with this code build-in:
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.display.MovieClip;
//VCam
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
function handleEnterFrame(event:Event):void {
if (parent) {
parent.scaleX = 1 / scaleX;
parent.scaleY = 1 / scaleY;
if (rotation == 0) {
parent.x = (width / 2 - x) / scaleX;
parent.y = (height / 2 - y) / scaleY;
parent.rotation = 0;
} else {
var bounds:Rectangle = getBounds(this);
var angle:Number = rotation * Math.PI / 180;
var midX:Number = -x / scaleX;
var midY:Number = -y / scaleY;
var rx:Number = -bounds.width / 2;
var ry:Number = -bounds.height / 2;
var cos:Number = Math.cos(angle);
var sin:Number = Math.sin(angle);
var rotatedX:Number = rx * cos - ry * sin;
var rotatedY:Number = ry * cos + rx * sin;
var cornerX:Number = midX - rotatedX;
var cornerY:Number = midY - rotatedY;
cos = Math.cos(-angle);
sin = Math.sin(-angle);
parent.x = cornerX * cos - cornerY * sin;
parent.y = cornerY * cos + cornerX * sin;
parent.rotation = -rotation;
}
}
}
addEventListener(Event.REMOVED, handleRemoved, false, 0, true);
function handleRemoved(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
removeEventListener(Event.REMOVED, handleRemoved);
}
When I jump with my character it seems that the minions follow the movement of my vcam and not behaving normally, jumping with the camera and falling throu'the ground when the character falls.
If I add a child normally from the main timeline by addChild(m_clip); it does not behave like that.
Is there an easy fix? Thanks!
This is the minions class code:
package {
import flash.display.*;
import flash.events.*;
public class EnemySpawned extends MovieClip {
protected var gravitysp: Number = 1;
protected var ySpeedsp: Number = 0;
protected var Speedsp: Number = 6.5;
var charMTL:MovieClip;
public function EnemySpawned()
{
this.addEventListener(Event.ENTER_FRAME, movement);
trace('exist');
}
function movement(event:Event):void
{
var MTL:MovieClip = MovieClip(root);
charMTL = MTL.char1;
ySpeedsp += gravitysp;
if(! MTL.ground_1.hitTestPoint(this.x, this.y, true))
{
this.y += ySpeedsp;
}
if(ySpeedsp > 40)
{
ySpeedsp = 40;
}
for(var j:int = 0; j<20; j++)
{
if(MTL.ground_1.hitTestPoint(this.x, this.y, true))
{
this.y--
ySpeedsp = 0;
}
}
var Distance:Number = charMTL.x - this.x;
if(Distance < -charMTL.width/2 - this.width/2)
{
this.x -= Speedsp;
}
if(Distance > charMTL.width/2 + this.width/2)
{
this.x += Speedsp;
}
}
}
}
There is no moving of ground_1 movieClip via code.
I want to create an object that follows and stops at mouse click. I managed to make it happen with rotation but the problem is that whenever i click on the empty stage, the object will move towards it and it carries on moving. It does not stop at the mouse location. Anyone know how i can do that. Below is my code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Guest extends MovieClip
{
var walkSpeed:Number = 5;
public function Guest()
{
stage.addEventListener(MouseEvent.CLICK, walk);
}
function walk(event:MouseEvent):void
{
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(event:Event):void
{
x = x+Math.cos(rotation/180*Math.PI)*walkSpeed;
y = y+Math.sin(rotation/180*Math.PI)*walkSpeed;
stage.removeEventListener(Event.ENTER_FRAME, loop);
}
}
}
Your code is a bit weird, here you will never move towards position for more than one frame since you remove the event listener as soon as loop is done.
Here is some code that fixes the moving and then stopping issue. However I strongly suggests that you do this with some kind of "tweening library" and I will after this show an example of doing that with Caurina Transitions.
function walk(e:MouseEvent):void {
targetX = parent.mouseX; //targetX created as a member variable
targetY = parent.mouseY; //targetY created as a member variable
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event):void {
var newX:Number = x + Math.cos(rotation / 180 * Math.PI) * walkSpeed;
var newY:Number = y + Math.sin(rotation / 180 * Math.PI) * walkSpeed;
var atTarget:Boolean = true;
if (Math.abs(targetX - newX) > walkSpeed) {
x = newX;
atTarget = false;
}
if(Math.abs(targetY - y) > walkSpeed) {
y = newY;
atTarget = false;
}
if (atTarget) {
stage.removeEventListener(Event.ENTER_FRAME, loop);
}
}
Here's the same behaviour with caurina.
package
{
import caurina.transitions.Tweener;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class TransitionExample extends MovieClip
{
private var targetX:Number;
private var targetY:Number;
var walkSpeed:Number = 5;
public function TransitionExample()
{
trace("ctor()");
stage.addEventListener(MouseEvent.CLICK, walk);
}
private function walk(e:MouseEvent):void {
targetX = parent.mouseX;
targetY = parent.mouseY;
var dx = targetX - x;
var dy = targetY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
var tweenDone:Function = function():void {
trace("tween is finished");
}
//modify time to be dependant on the "walkspeed" and the distance travelled etc...
Tweener.addTween(this, { x:targetX, y:targetY, time:0.458, transition:"linear", onComplete:tweenDone});
}
}
}
I am implementing this code for drawing the sound wave. It is in the adobe livedocs at soundmixer . My problem is how to widen up the sound wave? For example I would like it to be 655 pixels. I can change it to draw to different channels and also change the height of the drawing but cannot find how to change the width of the whole drawing
Any idea how to do that?
Thanks.
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
public class SoundMixer_computeSpectrumExample extends Sprite {
public function SoundMixer_computeSpectrumExample() {
var snd:Sound = new Sound();
var req:URLRequest = new URLRequest("Song1.mp3");
snd.load(req);
var channel:SoundChannel;
channel = snd.play();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
private function onEnterFrame(event:Event):void {
var bytes:ByteArray = new ByteArray();
const PLOT_HEIGHT:int = 25;
const CHANNEL_LENGTH:int = 256;
SoundMixer.computeSpectrum(bytes, false, 0);
var g:Graphics = this.graphics;
g.clear();
g.lineStyle(0, 0x6600CC);
g.beginFill(0x6600CC);
g.moveTo(0, PLOT_HEIGHT);
var n:Number = 0;
for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
g.endFill();
g.lineStyle(0, 0xCC0066);
g.beginFill(0xCC0066, 0.5);
g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
for (i = CHANNEL_LENGTH; i > 0; i--) {
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(0, PLOT_HEIGHT);
g.endFill();
}
private function onPlaybackComplete(event:Event):void {
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
}
change the x factor in your lineTo(x,y) calls.
for example, change:
g.lineTo(i * 2, PLOT_HEIGHT - n);
to something like:
var xfactor:Number = 655/256;
g.lineTo(i * xfactor, PLOT_HEIGHT - n);
since xfactor is fixed, calculate it before you enter your loops (not within them(