removeChild used incorrectly? - actionscript

I'm just trying to remove a movie clip from the stage upon double clicking
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
public class Main extends MovieClip{
var thingeh:Things;
public function Main() {
thingeh = new Things;
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(MouseEvent.DOUBLE_CLICK, onDouble);
}
public function onClick(event:MouseEvent)
{
addChild(thingeh);
}
public function onDouble(event:MouseEvent)
{
if(thingeh)
removeChild(thingeh);
}
}
}

public function onDouble(event:MouseEvent)
{
if( thingeh && contains(thingeh) )
{
removeChild(thingeh);
}
}
Also, You may want doubleClickEnabled = true within your constructor.
Do note, MouseEvent.CLICK gets fired TWICE along with a MouseEvent.DOUBLE_CLICK event.

Related

Xtext. Can't add HyperlinkHelper

I try to customize HyperlinkHelper. So I have override HypertextDetector
package org.xtext.example.mydsl.ui;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.xtext.ui.editor.hyperlinking.DefaultHyperlinkDetector;
import org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkHelper;
public class MyHyperlinkDetector extends DefaultHyperlinkDetector {
private static final String PREFERENCES = ".hyper";
#Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
IDocument document = textViewer.getDocument();
int offset = region.getOffset();
// extract relevant characters
IRegion lineRegion;
String candidate;
try {
lineRegion = document.getLineInformationOfOffset(offset);
candidate = document.get(lineRegion.getOffset(), lineRegion.getLength());
} catch (BadLocationException ex) {
return null;
}
// look for keyword
int index = candidate.indexOf(PREFERENCES);
if (index != -1) {
// detect region containing keyword
IRegion targetRegion = new Region(lineRegion.getOffset() + index, PREFERENCES.length());
if ((targetRegion.getOffset() <= offset)
&& ((targetRegion.getOffset() + targetRegion.getLength()) > offset))
// create link
return new IHyperlink[] { new PreferencesHyperlink(targetRegion, candidate) };
}
return null;
}
#Override
public IHyperlinkHelper getHelper() {
// TODO Auto-generated method stub
return new MyHyperlinkHelper();
}
}
Hyperlink detector is worked, but MyHyperlinkHelper is never created. Even if I comment method detectHyperlinks.
My goal is to open file with name what I have click in my edited dsl grammar. That's why I need HyperlinkHelper. I.e. I need to check does my substring is correct file name.
How to solve it?
Regards,
Vladimir.
dont override the method. simply use guice and call the method from the superclass in your impl
public Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
return DomainmodelHyperlinkHelper.class;
}
or in Xtend
def Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
return DomainmodelHyperlinkHelper;
}

connecting vertices in jung by edges results in another extra vertex being created

I am implementing an interface for taking commands for creating , connecting and coloring vertices in JUNG when I want to connect two already existing vertices JUNG connects to vertices and creates an extra vertex , why?
Here is my code for connect method:
public class Connect extends Command {
private CommandMaster cm;
private BehGraphUndirected behGraph;
private static int edgenumber=0;
#Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {
System.out.print("connect Runs\n");
this.cm = new CommandMaster();
this.behGraph = behGraph;
if(cm.exists(args[0]))
{
//got to another command
}else
{
switch (args[0]) {
case "edge":
this.createEdge(args[1] , args[2]);
break;
}
}
interpretMaster.refreshAndRepaint();
return null;
}
public void createEdge(String nodeName1 , String nodeName2)
{
this.behGraph.addEdge(edgenumber++,nodeName1, nodeName2);
System.out.println(this.behGraph.getVertexCount());
System.out.println("edge between: "+nodeName1+" and "+ nodeName2+" added");
}
And it's the create method just in case you want to know the way I implemented the code:
package interpreter.command;
import GraphHandling.BehGraphUndirected;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import interpreter.Command;
import interpreter.CommandMaster;
import interpreter.InterpretMaster;
/**
*
* #author Administrator
*/
public class Create extends Command{
private CommandMaster cm;
private BehGraphUndirected behGraph;
#Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {
System.out.print("create Runs \n");
this.cm = new CommandMaster();
this.behGraph = behGraph;
if(cm.exists(args[0]))
{
//got to another command
}else
{
switch (args[0]) {
case "node":
this.createNode(args[1]);
break;
case "label":
this.createLabel(args[1]);
break;
}
}
interpretMaster.refreshAndRepaint();
return null;
}
public void createNode(String nodeName)
{
this.behGraph.addVertex(nodeName);
System.out.print("vertex: "+nodeName+" added");
}
private void createLabel(String string) {
}
class str
{
int i;
long j;
}
}
Graph images before and after connecting two nodes:
and Here is my BehGraphUndirected class:
package GraphHandling;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.LinkedList;
/**
*
* #author Administrator
*/
public class BehGraphUndirected extends UndirectedSparseGraph{
private final LinkedList<Node> nodeList;
public BehGraphUndirected()
{
this.nodeList = new LinkedList<>();
}
public void addNode(Node newNode)
{
this.nodeList.add(newNode);
}
}
You should look at what BehGraphUndirected is doing; it's not a JUNG class or interface.
What is the name of the vertex that's being created, and how does that relate to what's being passed to the create method?
I have compiled and tested your code , The Jung library seems working right and It extinguishes the different nodes by the different object that was given to it It seems you have some other problem , Like a problem in processing the input strings that are used as objects that create nodes.

How to run another instance of Main() in AS3

How would you run a new instance of your Main function in AS3? Basically I want the program to rerun itself from the start, once it reaches the end of the program. I thought about removing all my childs from the tree at the end, then running the first method again, but that just seems unnecessary and messy. The class extends Sprite.
EDIT: Trying to create a Custom Event and using dispatchEvent to trigger this at the end of the program but having a little problem. This is my current code below.
Entire RunMain.as:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class RunMain extends Sprite {
public function RunMain():void {
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event:Event){
removeEventListener(Event.ENTER_FRAME, loop);
dispatchEvent(new EndEvent(EndEvent.END_EVENT));
}
}
}
Entire EndEvent.as:
package
{
import flash.events.Event;
public class EndEvent extends Event
{
// Event types.
public static const END_EVENT:String = "event1";
public function EndEvent(type:String = EndEvent.END_EVENT)
{
super(type);
}
override public function clone():Event {
// Return a new instance of this event with the same parameters.
return new EndEvent(type);
}
}
}
Parts of Main.as:
public function Main() {
runMain = new RunMain();
runMain.addEventListener(EndEvent.END_EVENT, onProgramFinish);
addChild(runMain);
init();
otherMethods();
}
public function onProgramFinish() {
removeChild(runMain);
runMain = null;
runMain = new RunMain() ;
}
Inside my Main class, I have the line onProgramFinish(); at the end of the program.
At the moment, when I run the compiler it comes up with the error:
ArgumentError: Error #1063: Argument count mismatch on Main/onProgramFinish().
Expected 0, got 1.
When I choose to continue, the program runs fine, but it doesn't run a new instance at the end of it.
In fact you must create a special class container and swap him (create new or delete) exactly in main class. This game class can dispatch events, at least sprites can dispatch events. Creating your custom events and passing them to higher and higher level will allow you to perform actions in Main class.
class Main extends Sprite {
public var game:Game ;
public function Main():void {
game = new Game(new Player(), "asd") ;
game.addEventListener(GameEvent.GAME_OVER, onGameOver) ;
addChild(game) ;
}
public function onGameOver(){
removeChild(game) ;
//also you can try removing heavy children of game
game = null ; //Let garbage collector know about object without reference.
game = new Game(new Player(), "example") ;
}
}
public class Game extends Sprite {
public var hero:Hero = new Hero() ;
public function Game(player:Player, name:String):void {
addEventListener(Event.ENTER_FRAME, loop) ;
}
private function loop(event:Event){
if (hero.hp == 0){
removeEventListener(Event.ENTER_FRAME, loop) ;
dispatchEvent(new GameEvent(GameEvent.GAME_OVER)) ;
}
}
}
Your event:
package {
import flash.events.Event;
public class ProgramEvent extends Event
{
public static const FINISH:String = "Finished" ;
public function ProgramEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
public override function clone():Event
{
return new ProgramEvent(type, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("ProgramEvent", "type", "bubbles", "cancelable", "eventPhase");
}
}
}
RunMain:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class RunMain extends Sprite {
public function RunMain():void {
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event:Event){
removeEventListener(Event.ENTER_FRAME, loop);
dispatchEvent(new ProgramEvent(ProgramEvent.FINISH));
}
}
}
And Main:
public function Main() {
startProgram() ;
init();
otherMethods();
}
public function onProgramFinish(event:ProgramEvent) {
runMain.removeEventListener(ProgramEvent.FINISH, onProgramFinish) ;
removeChild(runMain);
runMain = null;
startProgram() ;
}
public function startProgram(){
runMain = new RunMain() ;
runMain.addEventListener(ProgramEvent.FINISH, onProgramFinish);
addChild(runMain);
}

How to make the image translucent and change the bounds in JAVA using Eclipse Android API level 2?

I used the HelloItemizedOverlay code from Google to create and overlay item and it works but now I need to make the image translucent and change the bounds and I do not know how that works exactly.
I found the method getOpacity() but I do not know how to implement it in my ItemizedOverlay class or in my Map class. I am also aware of getIntrinsicHeight() and getIntrinsicWidth() methods but like the getOpacity() I do not know how to use it correctly. The code I have used for my ItemizedOverlay is :
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class ContourItemizedOverlay extends ItemizedOverlay {
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public ContourItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenter(defaultMarker));
mContext = context;
// TODO Auto-generated constructor stub
}
public int getOpacity()
{
return PixelFormat.TRANSPARENT;
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
}

Actionscript 3.0 - Trying to remove a child

Simple simple problem but I can't get it to work.
So I just have a class with a display object, a ball. I create instances of it from Main.as
and run a for loop to check if I hit the ball and if I do, I want to remove the object.
But I can't.
What's wrong with my code?
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite
{
private var clay:Clay;
private var clayCollection:Vector.<Clay> = new Vector.<Clay>;
private var crash:Crash;
private var crashCollection:Vector.<Crash> = new Vector.<Crash>;
private var timer:Timer = new Timer(0);
private var newClayTimer:Timer = new Timer(1000);
public function Main()
{
newClayTimer.start();
newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay);
stage.addEventListener(MouseEvent.CLICK, checkForHit);
}
private function checkForHit(e:MouseEvent):void
{
if (clayCollection.length > 0)
{
for (var i:int = 0; i < clayCollection.length; i++)
{
if (e.target.hitTestObject(clayCollection[i]))
{
clayCollection.splice(i, 1);
removeChild(clayCollection[i]);
}
}
}
}
private function addNewClay(e:TimerEvent):void
{
clay = new Clay();
addChild(clay);
clayCollection.push(clay);
}
}
}
Try something like this
for (var i:int = 0; i < clayCollection.length; i++) {
var clay:Clay = clayCollection[i];
if (e.target.hitTextObject) {
//seems to me all clays will hit test as true with the stage?
removeChild(clay);
clayCollection.splice(i, 1);
}
}
That way you know that the object that you're trying to remove is definitely the one that hit tested true.
I agree with Amy, listen for a click on the instance of clay, not the stage. You can still remove it from your array, something like this (untested code):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite
{
private var clay:Clay;
private var clayCollection:Vector.<Clay> = new Vector.<Clay>;
private var crash:Crash;
private var crashCollection:Vector.<Crash> = new Vector.<Crash>;
private var timer:Timer = new Timer(0);
private var newClayTimer:Timer = new Timer(1000);
public function Main()
{
newClayTimer.start();
newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay);
}
private function checkForHit(e:MouseEvent):void
{
// identify the target as Clay
var clay:Clay = e.target as Clay;
if(contains(clay)) removeChild(clay);
// remove it from your array
for each (var c:Clay in clayCollection) {
if (c == clay) clayCollection.splice(clayCollection.indexOf(c), 1);
}
}
private function addNewClay(e:TimerEvent):void
{
clay = new Clay();
clay.addEventListener(MouseEvent.CLICK, checkForHit);
addChild(clay);
clayCollection.push(clay);
}
}
}
When the program created a instance of the object and you mouse click it, the debugger threw this error.
Main Thread (Suspended: RangeError: Error #1125: The index 0 is out of range 0.)
Main/checkForHit
The fix is that you need to delimit the index of the loop as you slice it. Also the sequence of when you apply the splice and delimit is important.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite {
private var clay:Clay;
private var clayCollection:Vector.<Clay> = new Vector.<Clay>;
private var timer:Timer = new Timer(0);
private var newClayTimer:Timer = new Timer(1000);
public function Main(){
newClayTimer.start();
newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay);
stage.addEventListener(MouseEvent.CLICK, checkForHit);
}//END MAIN()
private function checkForHit(e:MouseEvent):void{
if (clayCollection.length > 0)
{
for (var i:int = 0; i != clayCollection.length; i++)
{
trace(i,clayCollection.length);
if (e.target.hitTestObject(clayCollection[i]))
{
removeChild(clayCollection[i]);
clayCollection.splice(i, 1);
i--;
}
}
}
}//END checkForHit()
private function addNewClay(e:TimerEvent):void{
clay = new Clay();
addChild(clay);
clayCollection.push(clay);
}//END addNewClay()
}//END MAINCLASS
}//END PACKAGE

Resources