Posting from AS3 to Twitter - actionscript-3

I've got a main menu and a game which loads as a separate swf from that menu. So far when the player enters their name at the start on the main menu I can get this name to post to Twitter. Also when the player completes the game I get post their time onto Twitter.
The problem is that I want to take the name posted from the main menu and dispatch it and only when the player completed the game post their time and name to Twitter.
Code in main menu to dispatch name:
[CODE]
var NameTextField:TextField = new TextField();
var MyFormat:TextFormat = new TextFormat();
addChild(NameTextField);
SubmitButton.addEventListener(MouseEvent.CLICK, SubmitClicked);
function SubmitClicked(e:MouseEvent)
{
dispatchEvent(new Event(NameTextField.text, true));
trace (NameTextField.text);
NameTextField.selectable = false;
}
[/CODE]
Code in game to receive name and post time to twitter.
[CODE]
navigateToURL(new URLRequest('http://twitter.com/home?status='+encodeURIComponent(+NameTextField.text+" completed Game"+' in a time of '+HourText.text+':'+MinuteText.text+':'+SecondText.text+'')),'_blank');
[/CODE]

In order to post to twitter you need to be authenticated.
I suggest you use an existing api like Tweetr by swfjunkie_com or Twitter-ActionScript-API by Denis Borisenko
Check this post for a sample code and for more details
http://www.redcodelabs.com/2012/02/actionscript3-twitter-api/

I assume you have twitter authentication already taken care of..
So when user enters the name on main menu you can save the name into save global variable in javascript like
var name = "some name";
and call ExternalInterface from as3 when user is done playing
like
ExternalInterface.call('getName'..
in js you should have a function like
function getName() { return name; }

Related

Track Share events to FirebaseAnalytics

I have a share function on the app that allows users to share app to others, but I want to track number of shares. The share item is on the action bar, not on the app dropdown menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
//some other code here for items search item
//share menu item
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
String playStoreLink = Constants.PLAYSTORE_LINK + getPackageName();
String shareText = "Install app" + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
I have managed to implement tracking on other events by simply adding code in onClick() events but share event doesnt have onClick.
Code for tracking events example
Bundle param = new Bundle();
param.putString("call_data", "1");
mFirebaseAnalytics.logEvent("calls_made", param);
I've been strugling with the exact challenge, the quick answer is NO.
BUT, you can create your own share dialog in which you can detect on which app the user chose to share and then use analytics as desired:
Find the solution here
Another solution with ShareActionProvider

save load error 1010 array objects with sharedObjects as3

i cant figure out this one so maby you guys can help me out.
i store some data in the form of a array filled with Objects in this example cards.
in my main class i have the following code:
deckSprite.savedData = SharedObject.getLocal("cardsdata");
deckSprite.savedData.data.savedArray = deckSprite.deckArr;
deckSprite.savedData.flush();
trace(deckSprite.savedData.data.savedArray);
the trace will output something like [object card1, object card2, object card3]
now in a static class called "deckSprite" i have this:
savedData = sharedObject.getLocal("cardsdata");
if (savedData.data.savedArray == undefined)
{
trace("no save yet");
}
{
else
{
trace("save loaded");
deckArr = savedData.data.savedArray;
trace(savedData.data.savedArray);
now my trace data turns out only ", ," (somehow the cards are gone).
now after i got saved data i restart my application and whenever he tryes to acces the deckArr it crashes giving me the error "A term is undefined and has no properties".
how is it possible that when i save the array it saves all the cards inside the array and when i restart the application its suddenly only ",,"but the cards are gone?
When serializing objects in AS3, you have to register their class using registerClassAlias() from package flash.net. So you have to call something like
registerClassAlias('com.example.deck', Deck)
in the program before any saving or loading happens.
See full reference at AS3 API Reference
NOTE: As pointed out by #BadFeelingAboutThis in comments, you have to register all referenced class in your Deck, i.e. if your deck looks like this:
class Deck {
var firstCard:Card;
var type:DeckType;
}
to be able to save Deck to SharedObject you have to call
registerClassAlias('com.example.deck', Deck);
registerClassAlias('com.example.card', Card);
registerClassAlias('com.example.decktype', DeckType);
before any saving/loading is done.
EDIT
Everything depends on content of your array
If I assume, your deckSprite is declared like this:
var deck1:Deck = new Deck();
var deck2:Deck = new Deck();
var deckArr:Array = new Array(deck1, deck2);
var deckSprite:DeckSprite = new DeckSprite()
deckSprite.setDeckArr(deckArr);
then before adding deckArray to SharedObject, you have to call registerClassAlias(). Sou your save code will look like this:
registerClassAlias('com.example.deck', Deck);
deckSprite.savedData = SharedObject.getLocal("cardsdata");
deckSprite.savedData.data.savedArray = deckSprite.deckArr;
deckSprite.savedData.flush();
trace(deckSprite.savedData.data.savedArray);
(replace the Deck with actual class you use for representing your decks)
Similarly the first line has to be called before you do any loading.
Of course it is best not to repeat yourself, so you should call registerClassAlias('com.example.deck', Deck); only once in your program, so for example in some init() method in your main class, if you have something like that.

Trouble understanding the event flow of the Twitter for AS3 library by susisu

I'm new to AS3, really new, and all I'm trying to do is a simple call to the Search API to get 100 tweets with a specific hashtag working with this library.
So in my main class I have a twitter object and a function called retrieveTweetsByHashtag(hashtagSt:String). In this function I do:
var retrievedInfo:TwitterRequest = myTwitterObject.search_tweets('#'+hashtagSt,null,null,null,null,100);
But I'm not really getting how events work, I guess.
After that I traced retrievedInfo.data, retrievedInfo.response .
There I gathered that retrievedInfo.data contains the query elements, and retrievedInfo.response seems to be an empty string (though perhaps it shouldn't be?), so I don't know where to look for the data I'm trying to find.
Any help would be greatly appreciated.
You need to listen for the event that is being dispatched by TwitterRequest, in this case it appears to dispatch TwitterRequestEvent.COMPLETE when the request has completed:
var retrievedInfo:TwitterRequest = myTwitterObject.search_tweets('#'+hashtagSt,null,null,null,null,100);
// add the event listener
retrievedInfo.addEventListener(TwitterRequestEvent.COMPLETE, onInfoRetrieved);
// handle the event
function onInfoRetrieved(e:TwitterRequestEvent):void
{
// retrieve the response property from the target of the event (in this case, the target was retrievedInfo
var response:String = TwitterRequest(e.target).response;
}

AS3. How to set-up players for real-time game?

I'm creating flash fighting game 1vs1.
Here is Hero (local-player) and Enemy (remote-player). How I need to setup them correctly that after connection to arena they will be spawned successfully?
I mean if player 1 connects to arena he should be declared as Hero (local-player) and for him player 2 should look like Enemy (remote-player).
The same for player 2. He should be declared as Hero (local-player) and for him player 1 should look like Enemy (remote-player).
Here are 2 character's templates to choose and here is code:
public function selectHero(what:int):void {
// this is called with correct "what", design yourself. I use array index
var whatHero:Class = heroes[what]; // get selected hero symbol
if (Hero && Hero.parent) Hero.parent.removeChild(Hero);
// clean up previous hero. Drop listeners here, if any
Hero = new whatHero(); // get new hero
// process as usual, don't forget to "addChild(Hero)" somewhere
create_hero();
}
function choosePlayer(event:MouseEvent):void {
selectHero(0); // here choose first template
start(event);
}
function create_hero()
{
addChild(Hero);
}
So Hero added to stage (It is local-player).
This is how I declare Enemy:
public var Enemy:Priesas = new Priesas; //Priesas is instance name of Enemy
So as I understand I don't need to use addChild(Enemy); because will be added just template, how to add remote-player Hero (from other computer) that will be declared as Enemy? Or something like that.
This game is creating for Facebook. For that is needed AppWarp? Thank you for answers.
Yes, you would need AppWarp to connect the two players and to exchange messages between them. This seems similar to one of the samples of AppWarp (smiley space shooter). Have you already explored the samples and documentation?
http://appwarp.shephertz.com/game-development-center/actionscript3-game-developers-home/

Reading in a File (AS3) and repeatedly/dyamically accessing the data

This may be a tired old question, but I have yet to find a good answer. Say for instance you have a class that reads in an xml file to get information such as a grocery store items, prices, etc. This class also allows you to retrieve the information about a grocery store item with a get() function.
var grocery:GroceryStore = new GroceryStore(); //create a class that
//reads in xml about
//grocery items
grocery.get("lettuce"); //get some data
In this scenario, I am constantly running into issues because the get() function is being called before the event that loads in the xml file. It wouldn't make sense to place the get() in the onLoad event for the xml file because I want it to be re-usable and dynamic. Also, AS3 doesn't have a wait() function so I can't stall until the file is loaded? Does anyone have an idea on how to read in a file and then be able to safely access the data dynamically and repeatedly? Hopefully this example and my question is thorough enough, if not let me know.
Thanks
You can use events - listen for the complete event to be dispatched.
Add the following code to GroceryStore class
//constructor or a load method
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest(xmlurl));
function onLoad(e:Event):void
{
//process xml here
dispatchEvent(e);
}
Now use it as:
var grocery:GroceryStore = new GroceryStore();
grocery.addEventListener(Event.COMPLETE, onGroceryLoad);
function onGroceryLoad(e:Event):void
{
grocery.get("lettuce");
}