1120:access of undefined property playWithTouch - actionscript-3

I think my code is ok, but for some odd reason it doesn't work properly.
I am new to this AS3 but trying my best to make it work.
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.display.NativeWindow;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.AccelerometerEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.system.System;
import flash.desktop.SystemIdleMode;
import flash.sensors.Accelerometer;
import flash.utils.Timer;
public class Shooter extends MovieClip {
private var welcomeScreenMC: Welcome = new Welcome();
public function Shooter() {
// constructor code
stage.addChild(welcomeScreenMC);
welcomeScreenMC.x = stage.stageWidth / 2;
welcomeScreenMC.y = stage.stageHeight / 2;
welcomeScreenMC.buttonMC.addEventListener(MouseEvent.CLICK, playWithTouch);
}
}
}

Related

as3 Drawing using BitmapData

I am extremely new to actionscript (or any code for that matter) and am having some trouble undertsanding why my code wont work. I am trying to create a small drawing application using BitmapData - there are no errors when I run the code but it doesn't do anything at all.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var canvas:BitmapData = new BitmapData(100, 100, false, 0x009900);
stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
function draw(){
canvas.setPixel(mouseX, mouseY, 0x000000);
}
Hopefully this isn't too silly of a question - but if someone can guide me through why this wouldn't work or give me a working example (would be even better) I would be very grateful
You have to add the bitmapdata to the displayObject you are on (it should be a sprite)
private var bitmap:Bitmap;
//on your init function
bitmap = new Bitmap(canvas);
this.addchild(bitmap);
stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
function draw(e:MouseEvent)
{
bitmap.bitmapdata.setPixel(e.localX, e.localY, 0x000000);
}

New Tween not working correctly

A bit confused as to where Im going wrong here
I have a dynamic text field, called myText.
Im running the below code:
package {
import fl.transitions.Tween;
import fl.transitions.easing.Elastic;
import flash.display.MovieClip;
import fl.transitions.easing.*;
import flash.text.TextField;
import flash.text.TextFormat;
public class video extends MovieClip {
public function video() {
var fmt:TextFormat = new TextFormat();
var letterTween:Tween = new Tween(fmt, "letterSpacing", Elastic.easeInOut, 6, 15, 2, true);
myText.setTextFormat(fmt);
}
}
}
I know its targeting the text as the text letter spacing is set to 6 when running, but nothing happens, I dont get me nice tween to 15 letter spaces(ing)
where am I going wrong?
thanks
Andrew
The reason you don't see any updates is because when TextFormat properties change, you have to reapply the TextFormat. All you need to do is listen for updates from the Tween and apply it there.
My example was tested on the timeline in CS5, go ahead and amend your class accordingly.
import fl.transitions.Tween;
import fl.transitions.easing.Elastic;
import flash.display.MovieClip;
import fl.transitions.easing.*;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.transitions.TweenEvent;
var fmt:TextFormat = new TextFormat();
var letterTween:Tween = new Tween(fmt, "letterSpacing", Elastic.easeInOut, 6, 15, 2, true);
letterTween.addEventListener(TweenEvent.MOTION_CHANGE, onMotionChanged);
myText.setTextFormat(fmt);
function onMotionChanged(event:TweenEvent):void{
myText.setTextFormat(fmt);
}

Implementing stageWebView in GestureWorks TouchSprite

I am attempting to create a component in a GestureWorks AIR application that renders a stageWebView in a touchsprite container. Given the stageWebView is not a display object is this possible?
Below is the code:
package view
{
import com.gestureworks.core.GestureWorks;
import com.gestureworks.core.TouchSprite;
import com.gestureworks.events.GWGestureEvent;
import com.modestmaps.Map;
import com.modestmaps.events.MapEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class webView extends TouchSprite
{
private var urlAddress:String = "http://google.com";
public var stageWebView:StageWebView = new StageWebView();
private var ts:TouchSprite;
public function webView()
{
super();
init();
}
private function init():void
{
ts = new TouchSprite();
ts.width = 500;
ts.height = 500;
ts.x = 200;
ts.y = 100;
ts.rotation = 45;
ts.scaleX = 0.5;
ts.scaleY = 0.5;
ts.gestureEvents = true;
ts.disableAffineTransform = false;
ts.gestureList = { "n-drag":true, "n-scale":true, "n-rotate":true };
ts.addEventListener(GWGestureEvent.DRAG, gestureDragHandler);
ts.addEventListener(GWGestureEvent.ROTATE, gestureRotateHandler);
ts.addEventListener(GWGestureEvent.SCALE, gestureScaleHandler);
if(StageWebView.isSupported == true)
{
stageWebView.stage = this.stage;
stageWebView.viewPort = new Rectangle(0,0, 500, 500);
//stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangedHandler);
//stage.scaleMode = StageScaleMode.EXACT_FIT;
//this.stage.addEventListener(Event.RESIZE, resizeEvent);
getURL();
}
else
{
urlAddress = "StageWebView not supported";
}
ts.addChild(stageWebView as DisplayObject);
addChild(ts);
}
protected function getURL():void
{
stageWebView.loadURL(urlAddress);
//stageWebView.addEventListener(Event.COMPLETE,handleLoad);
}
No. Since StageWebView is not a display object, you can't add it to another object to be managed automatically.
You can manage it manually from the sprite--when the sprite is displayed, show the web view, when the sprite is resized, resize the web view, etc.
If your web view is covering up the entire TouchSprite once it's rendered, I'm fairly certain that your sprite won't receive gesture events--those would be handled by the native component.

Health Bar not 100% full

I have a health bar that, on entering the frame, it decreases slightly and isnt full. How can I fix this? The "Should be" represents what I want it to look like. The "What it currently is" represents the problem I'm having.
Should be:
What is currently is:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.text.Font;
import flash.events.MouseEvent;
import flash.text.TextDisplayMode;
import flash.text.TextExtent;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.motion.easing.Back;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.ui.Mouse;
import flash.events.TimerEvent;
public class healthBar extends MovieClip
{
public var maxHP:int = 670;
public var currentHP:int = maxHP;
public var percentHP:Number = currentHP / maxHP;
public function healthBar()
{
updateHealthBar();
}
public function updateHealthBar()
{
percentHP = currentHP / maxHP;
scaleX = percentHP;
}
}
}
If you have print statement that outputs the percentHP variable, does it, in fact, display the correct value? 1 in this case, I believe.
You could also manually set scaleX to 1 and see if that still yields a non-filled bar.
Depending on the results of the two tests, you can narrow the issue down to how scaleX is being used, or how percentHP updates.

How to access dynamic text field on stage from within the document class

I'm trying to access a text input that I've already placed on the stage (inside a movie clip) but with no luck.
I've defined an instance name for this dynamic text field which is currentUserCount
I've got something like this set up in the document class actionscript file:
package {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class myProject extends Sprite {
public function myProject() {
// Trying stuff like
trace(currentUserCount);
trace(movieClipName.currentUserCount);
trace(root.currentUserCount);
}
}
}
What am I missing?
When I run this I get:
1120: Access of undefined property currentUserCount.
1120: Access of undefined property movieClipName.
1119: Access of possibly undefined property movieClipName through a reference with static type flash.display:DisplayObject.
1120: Access of undefined property currentUserCount.
If all of the movieclips on the stage had the same nested clip inside of it, you could also just reference the inner clips like so:
for (var i=0; i<stage.numChildren; i++){
var mc = stage.getChildAt(i)
mc.subClip.play()
}
Make sure the MovieClip and the TextField have instance names in your .fla file.
// MovieClip instance name: mc;
// Textfield instance name: tf;
Then in your document class:
package {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class myProject extends Sprite {
public function myProject() {
mc.tf.text = "Text you wanna see";
trace(mc.tf); // [Object TextField]
}
}
}
My problem was that I was referencing a movie clip within a movie clip and I didn't know I had to create an instance name for each one and work my way in code through each one.
I thought the instance names were a part of a global namespace.
So I was doing something like:
myMovieClip.play();
when I should have been doing:
mainMovieClip.subMovieClip.myMovieClip.play();