Gaming Your Way

May contain nuts.

Mochibot and BitmapData.draw() issues

Nice descriptive title for a change, mainly 'cause I've spent ages hunting around for a solution myself, so if you're coming here for just this very reason, I understand your pain.

We had issues with mochibot on Invaders Must Die, but by then I'd passed it over to Mousebreaker so they had to deal with it in-house. It's the first time I've hit it face on so to speak.

To set the scene and get you in the mood, I've just finished the viral version of cronusX and wanted to try the version control that mochi offer, as that was one of the best things about gameJacket, plus I've added a twitter option to the game which I know is going to bite me on the arse with people bitching it's spyware etc. etc. so I wanted the option to rip that out if it was becoming too much of a ball ache.
I've also included mochi-bot tracking so Candystand can see how it's doing out in the wild, so I've dropped that in too.

Everything worked first time ( That's where it differs from gameJacket and it's constant security check failures ), gave it a quick test, all good. Ah, not all good, the transition craps out to a black screen.

Testing it locally ( Turns out I've not installed the browsers debug version of FP10 on the pc, how good am I ? ) it spat out an error,

Error # 2121: Sandbox Security Violation: BitmapData.draw

etc.

Joy.

Lots of googling ( To no real avail, hence this post ), seeing the only advice on the mochi forums that I could find was someone copying out some text from the Flash help ( Cheers for that, solves everything ) it turns out that the mochi-bot can't be on your root display object, as you get all the security violations when trying to use draw(); that you would if you tried loading in say an image and using draw(); on that ( It's a cross domain thing. To be honest life is too short to go into it, I could just copy from the as3 docs, but... ).

Basically, long story cut shorter, if you do something like:

MochiBot.track(this,"yourMagicNumber");

In your preloader ( Assuming that "this" is your main preloader class than extends MovieClip ) then you can't use draw(); in your main code.

To get around this, give it a movieclip instead, eg.

mochiBotHolder=new MovieClip();
stage.addChild(mochiBotHolder);
mochiBotHolder.addEventListener(Event.ADDED_TO_STAGE,triggerMochiBotTracking);

private function triggerMochiBotTracking(e:Event):void{
    mochiBotHolder.removeEventListener(Event.ADDED_TO_STAGE,triggerMochiBotTracking);
    Security.allowDomain("http://core.mochibot.com/my/core.swf");
    MochiBot.track(mochiBotHolder,"yourMagicNumber");
}

Now I'm not sure if you need the allowDomain(), but it works with it, and that's good enough for me after losing a good hour and a half to this little gem of a problem.
Because the mochibot code needs to hit loaderInfo.loaderURL via the clip you pass it, you have to add your holder mc to the stage, and then wait until it's actually there before calling MochiBot.track, otherwise you're in the land of null properties.

Now I'm not sure if you can kill that holder mc of after calling the track() method, it's like the allowDomain(), it works as it is, and I'm happy with that.

This has proved to be a fairly techy / geeky post, quite rare from me.

Squize.

Comments (6) -

  • Patrick

    7/22/2009 1:11:37 PM |

    Wow what a PITA to figure out, nice detective work there.

    The idea of pushing stuff out to twitter from a game is pretty interesting - did you end up completely pulling this feature? Also, any chance of sharing that code?

  • Squize

    7/22/2009 2:23:58 PM |

    Hey Patrick, hope the post is of some help.

    The twitter stuff is still in there as the viral doesn't go live for around a week and a half I think.
    It actually took longer to write the disclaimer than to do the code ( I'm really expecting a backlash with people thinking we're up to all kinds of no good, hence the need for version control if it does turn sour ).

    I used this:
    swxformat.org/documentation/swx-apis/twitter-api

    Which is excellent, it does all the hard lifting for you. Literally took 10 mins to get it in and working, which isn't the usual coders 10 mins ( ie an hour ).

  • chrisError

    7/22/2009 10:59:30 PM |

    HA!

    Guess what? Really Guess Fucking What! I read your blog posting quickly, didn't notice any crude sexual references or childish behavior and so just thought 'Oh well, I'll just check back tomorrow'..but first.....I'll send this build off to a client....and guess FUCKING WHAT!! I get an email backing saying the version I sent over, the 'Gold' build, craps out on a transition, which I realise uses .draw and it's cos I just added the Mochibot.

    Copy and Pastes your code and now it works!

    The 22nd of July 2009 will go down in history as the day GYW Blog was an honest to God useful technical Flash resource.

    ChrisErrorx

  • Squize

    7/22/2009 11:47:51 PM |

    No Fucking way!

    Not the weird synchronicity, you accusing us of being a useful resource. I feel like I've undone two and half years of hard work just bitching about things, laughing at stuff which by any level of decency shouldn't even be spoken of and just generally hiding our lack of ambition and talent behind swear words.

    We're undone.

  • Ranoka

    7/28/2009 11:56:34 AM |

    Thanks for sharing this, I have a feeling with my current game I could have problems like this come up, because my game is built around blitting bitmap data. So will come back here if I have a problem with that when I try adding Mochi.

  • Squize

    7/29/2009 9:51:42 PM |

    Glad to be of help mate.

Comments are closed